Here is a very simple example of how to setup a couple of buttons and event-hand
ID: 638779 • Letter: H
Question
Here is a very simple example of how to setup a couple of buttons and event-handling. Can we modify the code( or pseudocode) to do the event-handling in an inner class?
If so, how would you modify it?
Class ButtonExample implements ActionListener{
JButton button1 = new JButton ("Button 1");
JButton button2 = new JButton ("Button 2");
public void actionPerformed (ActionEvent event) {
System.out.printIn ("I listened");
}
button1 . addActionListener (this);
button2 . addActionListener (this);
}
Explanation / Answer
You shouldn't be doing these things in a static method like main(); set things up in a constructor. I didn't compile or run this to see if it actually works, but give it a try.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.