- 论坛徽章:
- 0
|
人家的例子是这样的,看全乎了。
第一个例子:
import java.awt.*;
import java.awt.event.*;
/** A listener that you attach to the top-level Frame or JFrame of
* your application, so quitting the frame exits the application.
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
第二个:
import javax.swing.*;
import java.awt.*;
/** A few utilities that simplify using windows in Swing.
* 1998-99 Marty Hall,
*/
public class WindowUtilities { // look and feel
/** Tell system to use native look and feel, as in previous
* releases. Metal (Java) LAF is the default otherwise.
*/
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);
}
}
/** A simplified way to see a JPanel or other Container.
* Pops up a JFrame with specified Container as the content pane.
*/
public static JFrame openInJFrame(Container content,
int width,
int height,
String title,
Color bgColor) {
JFrame frame = new JFrame(title);
frame.setBackground(bgColor);
content.setBackground(bgColor);
frame.setSize(width, height);
frame.setContentPane(content);
frame.addWindowListener(new ExitListener());
frame.setVisible(true);
return(frame);
}
/** Uses Color.white as the background color. */
public static JFrame openInJFrame(Container content,
int width,
int height,
String title) {
return(openInJFrame(content, width, height, title, Color.white));
}
/** Uses Color.white as the background color, and the
* name of the Container's class as the JFrame title.
*/
public static JFrame openInJFrame(Container content,
int width,
int height) {
return(openInJFrame(content, width, height,
content.getClass().getName(),
Color.white));
}
}
第三个:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Tiny example showing the main difference in using
* JFrame instead of Frame: using the content pane
* and getting the Java (Metal) look and feel.
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class JFrameExample {
public static void main(String[] args) {
WindowUtilities.setNativeLookAndFeel();
JFrame f = new JFrame("This is a test");
f.setSize(400, 150);
Container content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
f.addWindowListener(new ExitListener());
f.setVisible(true);
}
}
把这仨放在一个文件夹里,javac *.java,就ok啦。。。 |
|