When the objects are in the window, we need to tell the window to get itself ready and then display itself on the screen. So far, we have seen how to declare, instantiate, fill, pack and show a window. Usually, when you use windows on a screen you click a little symbol in the top of the window to close it. Clicking a symbol with the mouse is an example of an asynchronous event. Since this can happen at any time, there has to be code in the program to handle this event.
Event listeners are objects whose roles are to wait for events to happen.
By convention, the method that registers a listener is called add followed by the listener name, which is WindowListener for window events. The parameter for the addWindowListener method is an instance of a class WindowAdapter plus a declaration block that contains the method windowClosing. All these classes and methods that are needed to handle window-closing events are in package java.awt.event. To use them, we must add the statement import java.awt.event. Write the staments necessary to declare, instantiate and display a window on the screen.