Hey guys I'm having problems with a Java assignment that I've been working on for weeks. Hopefully someone can steer me in the right direction. My assignment is to make the following changes to the program below:
(I already finished this but the first part is too...) change the blueButton so that it's event handling is done by an anonymous inner class. The ActionListener interface will serve as the "base class" for the anonymous inner class (this is okay, even though interfaces cannot be instantiated). Within the anonymous class definition you will specify the required event-handling method, which contains a couple of lines of code to change the color.
Then change the yellowButton so that an instance of a "top level" class (not an inner class) which you create, called ButtonListener, will do the event handling for it. You will need to define the class, and also create an instance of it, passing to the constructor the reference to the ButtonPanel (the "this" object). The constructor should save this reference in an instance variable. This reference is needed for a "callback" when the event is being handled. That is, the calls to setBackground() and repaint() should be done on the ButtonPanel object, not the ButtonListener object. After all, your listener object has no background - it's not even a visible object! It needs to set its creator object's background. That's why you saved the reference to the ButtonPanel in the ButtonListener's constructor.
and last, change the redButton so that it uses an named "inner" class to the ButtonPanel. This is very handy, since the setBackground and repaint methods can both use the implied object of "this" (the ButtonPanel). If the listener is an inner class, then it automatically has access to its enclosing class' "this". So with the inner class, there is no need to pass the ButtonPanel's reference as with the "top-level" class. Of course, you still need to create an instance of the inner class, and register it as the listener for the redButton.
import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public ButtonPanel() //constructor { yellowButton = new JButton("Yellow"); blueButton = new JButton("Blue"); redButton = new JButton("Red"); add(yellowButton); add(blueButton); add(redButton); yellowButton.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); } public void actionPerformed(ActionEvent evt) //ActionListener method { Object source = evt.getSource(); Color color = getBackground(); if (source == yellowButton) color = Color.yellow; else if (source == blueButton) color = Color.blue; else if (source == redButton) color = Color.red; setBackground(color); repaint(); } private JButton yellowButton; private JButton blueButton; private JButton redButton; } class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("Button Test"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = getContentPane(); contentPane.add(new ButtonPanel()); } } public class ButtonTest { public static void main(String[] args) { JFrame frame = new ButtonFrame(); frame.show(); } }
Here is my code. I'm stuck at the second set of instructions with the yellowButton and ButtonListener class. I made comments to show everything I was trying to do but I honestly have no clue what I'm doing. My compile errors are below. I changed up the code a bit from the original because I'm used to seeing the main method first and seeing class variables declared at the top instead of the bottom.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest2 { public static void main(String[] args) { JFrame frame = new ButtonFrame(); frame.show(); } } class ButtonListener implements ActionListener //"top-level" class that implements the ActionListener interface for the event handling. { private ButtonPanel thePanel; //object variables declared private ButtonPanel Pan; private ButtonListener l; public ButtonListener(ButtonPanel Pan) //constructor that passes the reference to the ButtonPanel object (the "this" object) { thePanel = Pan; //instance varible to save reference to theButtonPanel object ButtonListener l = new ButtonListener(thePanel); //new ButtonListener object to do the event handling. I am referencing to the ButtonPanel object in the parentheses. l.addActionListener(); //call to event handler } public void actionPerformed(ActionEvent event) //ActionListener method to do the event handling { l.setBackground(Color.yellow); l.repaint(); } } class ButtonPanel extends JPanel { private JButton blueButton; private JButton yellowButton; private JButton redButton; public ButtonPanel() { JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); add(yellowButton); add(blueButton); add(redButton); blueButton.addActionListener(new ActionListener() //annonymous class to perform the event handling for the blueButton { public void actionPerformed(ActionEvent event) { Object blueButton = event.getSource(); setBackground(Color.blue); repaint(); } }); } } class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("Button Test"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = getContentPane(); contentPane.add(new ButtonPanel()); } }
Here are the errorsjava:33: cannot find symbol symbol : method addActionListener() location: class ButtonListener l.addActionListener(); ^ java:39: cannot find symbol symbol : method setBackground(java.awt.Color) location: class ButtonListener l.setBackground(Color.yellow); ^ java:40: cannot find symbol symbol : method repaint() location: class ButtonListener l.repaint(); ^ Note: ButtonTest2.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 3 errors