CPT-237

Programming Assignment: Event Handling

  1. 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.

  2. 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.

  3. 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.