Java Reference
In-Depth Information
In the ExitButton header after extends Button , add the following:
3.
implements ActionListener
Notice no error messages were generated because we already coded the actionPerformed method!
4.
In the ExitButton constructor, enter the following statement:
this .addActionListener( this );
At this point, you have defined and assigned an action listener to the exit button. Now we need to define what
should be done when the exit button is clicked. Can you figure out where to add that code?
5.
In the actionPerformed method, add the following statement:
System.exit(0);
You may not remember, but earlier we availed ourselves of the very useful System class. The System class had an
object ( out) whose method ( println ) was used to display text in the console. The System class also has an exit method that
ends the JVM. The result of the exit method (the frame disappears) seems the same as the result of the dispose method;
however, this is because our application is relatively simple. If multiple frames in the application had been displayed,
exit would destroy all of them (plus any nonvisual objects). The dispose method only destroys the object it is used in.
6.
Save the ExitButton source code and run EmployeeApp.
7.
Click the Exit button.
Yay, a working button!
Figure 4-4 shows the Employee classes, their relationships to each other, and their relationships to some of
the JRE classes. Notice the new implements relationship, shown as a dotted line and an open triangle arrowhead
(e.g., between UsefulFrame and WindowListener and ExitButton and ActionListener).
Figure 4-4.
Search WWH ::




Custom Search