Java Reference
In-Depth Information
their access specifiers. Let's change the structure of the Sketcher class once more to make use of an adapter
class.
TRY IT OUT: Implementing an Adapter Class
The version of the Sketcher class to implement this is as follows, with changes to the previous version
highlighted:
// Sketching application
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sketcher {
public static void main(String[] args) {
theApp = new Sketcher(); // Create the application
object
SwingUtilities.invokeLater(new Runnable() {
public void run() {
theApp.creatGUI(); // Call
static GUI creator
}
});
}
// Method to create the application GUI
private void creatGUI() {
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(new WindowHandler());
// Add window
listener
window.setVisible(true); // Display
the window
}
// Handler class for window events
class WindowHandler extends WindowAdapter {
// Handler for window closing event
Search WWH ::




Custom Search