Java Reference
In-Depth Information
In lines 83 through 91 in Figure 5-29, the program overrides the
windowClosing() method, telling the program what to do when the user
clicks the Close button in the Frame. Normally, the windowClosing() method
simply closes the window or Frame, making it invisible, but does not necessarily
stop the program. In the Reservations() method, however, it is overridden so
that the program terminates and the window closes.
Internally, Java sends this windowClosing() method a WindowEvent object
when the title bar Close button is clicked. The method disposes of the current
Frame object and then calls System.exit() in line 88 to exit the application.
In previous chapters, you used the System.out() method to direct the operat-
ing system to send a stream of characters to the default output device, usually the
monitor. When encountered, the System.exit() method causes the operating system
to terminate the program. System.exit() has one argument, which is sent to the
operating system in which the program is running. Traditionally, System.exit()
returns a zero (0) to the operating system when a program ends normally and a
one (1) when a program ends due to an error. The System.exit() method is used
with applications rather than applets. If necessary, the System.exit() method's
argument is used by the JVM when the application terminates.
The following step enters code to add the addWindowListener() method and
override the windowClosing() method to close the window.
To Code the Window Event-Handling Method
1. Enter lines 82 through 93, as shown in Figure 5-29.
The TextPad window displays the code for the addWindowListener()
method (Figure 5-30). The addWindowListener() method creates an
occurrence of the WindowAdapter class. Lines 83 through 91 override
the windowClosing() method, so that clicking the title bar Close button
terminates the program.
overrides
windowClosing()
method
creates occurrence
of WindowAdapter
class
addWindowListener()
method
FIGURE 5-30
 
Search WWH ::




Custom Search