Java Reference
In-Depth Information
private void createGUI() {
window = new SketcherFrame("Sketcher"); // Create
the app window
Toolkit theKit = window.getToolkit(); // Get the
window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get
screen size
// Set the position to screen center & size to half screen size
window.setSize(wndSize.width/2, wndSize.height/2); // Set
window size
window.setLocationRelativeTo(null); // Center
window
window.addWindowListener(this);
// theApp as
window listener
window.setVisible(true);
}
// Handler for window closing event
public void windowClosing(WindowEvent e) {
window.dispose();
// Release the
window resources
System.exit(0);
// End the
application
}
// Listener interface methods you must implement - but don't need
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
private SketcherFrame window;
// The
application window
private static Sketcher theApp;
// The
application object
}
Directory "Sketcher 2 implementing a low-level listener"
If you run the Sketcher program again, you see it works just as before, but now the Sketcher class object
is handling the close operation.
How It Works
You have added an import statement for the names in the java.awt.event package, which includes Win-
dowEvent and WindowListener . The Sketcher class now implements the WindowListener interface,
so an object of type Sketcher can handle window events. The main() method now creates a Sketcher
object and stores the reference in the static class member theApp .
Search WWH ::




Custom Search