This code is total chaos.
- you are extending JPanel and then using JFrame to build your GUI.
delete the "extends JPanel" or use "extends JFrame" and refer to it via "this".
public class MyFrame extends JFrame{
public myFrame(){
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EX…
this.setTitle("BULLS EYE");
JButton oButton = new JButton("Click me hard!");
oButton.addActionListener(new ButtonAction());
this.add(oButton);
}
public static void main (String args []) {
MyFrame oFrame = new MyFrame();
oFrame.setVisible(true);
}
}
- you are creating static classes for the Actions because you are too lazy to move the JFrame out of the main method.
Change that.
public class MyFrame extends JFrame{
public ButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
}
For the Action:
You can refer to the original object (the JButton) via
oEvent.getSource()
. You will need to cast that given Object that to get the JButton back:
public ButtonAction implements ActionListener{
public void actionPerformed(ActionEvent oEvent) {
if(oEvent.getSource instanceOf JButton){
JButton oButton = (JButton)oEvent.getSource();
MyFrame.this.doFunnyThings();
}
}
}
}
You can also define a ActionCommand for the button to identify the trigger of the action.
Please do a little search on the web for that.