You can't simply add a listener to component who neither supports the listener you are trying to attach or to a component to whom you don't have a reference to.
That is, JFrame
doesn't support ActionListener
and you don't have a reference to the buttons you want to add actions to.
I know you've made the buttons public
, but to me, this is a bad idea, as you are exposing the components to outside modification. There's nothing stopping code from changing the state of the buttons, taking control away from the Frame
class.
Instead, you should provide the ability for interested parties to register interest in knowing when the buttons are activated, for example...
public static class Frame extends JFrame {
ImageIcon green = new ImageIcon("green.png");
JLabel label;
private JButton button1, button2;
public Frame() {
//...
}
public void addButton1ActionListener(ActionListener listener) {
button1.addActionListener(listener);
}
public void addButton2ActionListener(ActionListener listener) {
button2.addActionListener(listener);
}
public void removeButton1ActionListener(ActionListener listener) {
button1.removeActionListener(listener);
}
public void removeButton2ActionListener(ActionListener listener) {
button2.removeActionListener(listener);
}
Then you would simply add your ActionListener
to which ever button you want whenever you want to, for example...
public static void main(String[] args) {
Frame grid = new Frame();
grid.addButton1ActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do stuff
}
});
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grid.setVisible(true);
grid.pack();
grid.setTitle("Frame");
}
Now, this raises issues, because now you've provided access to the underlying button, which you might not want to.
Now, going beyond the question, I might suggest the use of some kind of model. This would be applied to Frame
, which would allow it to modify its state as it sees fit, which would then provide event notification back to interested parties stating that some state or other has changed and that they should take appropriate action, such as updating the view.
This is best described by the MVC pattern