In the previous lesson we looked at containers and JPanel.
Using Inner classes
Most classes you write are top-level classes, that is, independent classes in its own right and in their own class file. It is often useful to create inner classes, being classes that exist inside of another class. You would create an inner class when the outer class (i.e., the top-level class that contains it) requires a service that is specific to the outer class only. This is frequently the case when coding event listeners.
Inner classes are often declared private, though they don't have to be. You will now modify ExampleFrame to use an inner class as the listener for the centre button object, instead of the frame (the outer class) being the direct listener.
Step 1
Surround the actionPerformed() method with the inner class declaration (this should be indented the same as if it were another method, and therefore the actionPerformed() method should be indented one level further in that it is currently):
Step 2
Remove the implements ActionListener section from the outer class declaration so that it reads as follows:
Step 3
Change the constructor to read as follows:
Verify that the frame correctly responds to the West and Centre buttons.
In the next lesson we will define separate listeners for each event.