Page 187 - The Definitive Guide to Building Java Robots
P. 187
Preston_5564C06.fm Page 168 Friday, September 23, 2005 5:13 AM
168 CHAPTER 6 ■ VISION
// used in all SWING examples
public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}
public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}
public static void setMotifLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch(Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}
}
Next, I’ll create another utility class that will help the program exit in the event of closing
the window. Although trivial, it does save some time. See Example 6-2.
Example 6-2. ExitListener.java
package com.scottpreston.javarobot.chapter6;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
Now I’m ready to create a simple Swing component. In Example 6-2, the simple Swing
component will extend the JFrame object.
In the constructor, I set the title of the JFrame, then chose the look and feel, and then
added the Exit Listener. Next, I’ll set the size to a default 320×240 pixels, change the back-
ground of the content pane to white, and set the class to visible. See Example 6-3 and Figure 6-2.