public class Console
1: //: com:bruceeckel:swing:Console.java
2: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
3: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
4: // Tool for running Swing demos from the
5: // console, both applets and JFrames.
6: package com.bruceeckel.swing;
7: import javax.swing.*;
8: import java.awt.event.*;
10: public class Console {
11: // Create a title string from the class name:
12: public static String title(Object o) {
13: String t = o.getClass().toString();
14: // Remove the word "class":
15: if(t.indexOf("class") != -1)
16: t = t.substring(6);
17: return t;
18: }
19: public static void setupClosing(JFrame frame) {
20: // The JDK 1.2 Solution as an
21: // anonymous inner class:
22: frame.addWindowListener(new WindowAdapter() {
23: public void windowClosing(WindowEvent e) {
24: System.exit(0);
25: }
26: });
27: // The improved solution in JDK 1.3:
28: // frame.setDefaultCloseOperation(
29: // EXIT_ON_CLOSE);
30: }
31: public static void
32: run(JFrame frame, int width, int height) {
33: setupClosing(frame);
34: frame.setSize(width, height);
35: frame.setVisible(true);
36: }
37: public static void
38: run(JApplet applet, int width, int height) {
39: JFrame frame = new JFrame(title(applet));
40: setupClosing(frame);
41: frame.getContentPane().add(applet);
42: frame.setSize(width, height);
43: applet.init();
44: applet.start();
45: frame.setVisible(true);
46: }
47: public static void
48: run(JPanel panel, int width, int height) {
49: JFrame frame = new JFrame(title(panel));
50: setupClosing(frame);
51: frame.getContentPane().add(panel);
52: frame.setSize(width, height);
53: frame.setVisible(true);
54: }
55: } ///:~