Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote