Your problem is related to the limited scope of variables. Your startButton variable is declared in the constructor and therefore is visible only in the constructor. You need to declare it in the class without re-declaring it in the constructor, allowing the rest of the class to βseeβ the variable and use it.
ie, change this:
public class PipeGameApp extends JFrame implements ActionListener { private static int BOARD_SIZE = 11; private PipeGame game;
:
public class PipeGameApp extends JFrame implements ActionListener { private static int BOARD_SIZE = 11; private PipeGame game;
As an alternative:
- use jtogglebutton
- Or use the object returned by the ActionEvent
getSource() method and set its new state based on its current state.
For example,
@Override public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if (source instanceof JButton) { if (ae.getText().equals("Start")) { ae.setText("Stop"); // do other stuff } else if (ae.getText().equals("Stop")) { ae.setText("Start"); // do more stuff } } }
Regarding "I might be missing some "}" because I left out a lot of the code that irrelevant." Please make an effort to prevent this from happening. The missing "}" should not be missing, and it becomes more difficult for us to understand your code and help you. If you ask others to make an effort to help you in your free time, it does not ask you too much not to send junk code.
Hovercraft full of eels
source share