Sunday, 29 September 2013

Java - Actionlistener showing error for a reason I can't see

Java - Actionlistener showing error for a reason I can't see

In the if statement, it is failing to find launchBtn. I'm probably doing
something stupidly obvious. Can anyone see what's wrong? The errors are in
bold (or highlighted with two **, Here is my code:
package launcher;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
class Window extends JFrame implements ActionListener
{
JPanel panel = new JPanel();
public Window()
{
//Creates the blank panel
super("Launcher");
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
setVisible(true);
//Create the button variables
JButton launchBtn = new JButton("Launch Game");
JButton optionsBtn = new JButton("Launcher Options");
//Add the buttons to the launcher
panel.add(launchBtn);
panel.add(optionsBtn);
//Add the buttons to the action listener
launchBtn.addActionListener(this);
optionsBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == **launchBtn**)
{
**launchBtn**.setEnabled(true);
}
}
}

No comments:

Post a Comment