Once you understand the general explanation in Segment 684, you are ready for the details.
The first step in preparing an application to respond to a click on a
window's close button is to
define a listener class, such as the
following. The WindowListener
interface requires you to define
various methods, one of which is the windowClosing
method. You
define that method to shut down the application; you define the others with
empty, do-nothing bodies.
Note that you must import the classes in the java.awt.event
package whenever you want to define a class that implements the
WindowListener
interface.
import java.awt.event.*;
public class ApplicationClosingWindowListener implements WindowListener {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}