01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package org.quickserver.swing;
16:
17: import javax.swing.*;
18: import java.awt.event.*;
19: import java.awt.Window;
20: import java.awt.Toolkit;
21: import java.awt.Dimension;
22:
23: /**
24: * Swing utility class
25: */
26: public class JFrameUtilities {
27:
28: /**
29: * Create a title string from the class name.
30: */
31: public static String title(Object o) {
32: String t = o.getClass().toString();
33: // Remove the word "class":
34: if (t.indexOf("class") != -1)
35: t = t.substring(6);
36: if (t.lastIndexOf(".") != -1)
37: t = t.substring(t.lastIndexOf(".") + 1);
38: return t;
39: }
40:
41: public static void run(JFrame frame, int width, int height) {
42: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43: frame.setSize(width, height);
44: frame.setVisible(true);
45: }
46:
47: public static void run(JApplet applet, int width, int height) {
48: JFrame frame = new JFrame(title(applet));
49: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50: frame.getContentPane().add(applet);
51: frame.setSize(width, height);
52: applet.init();
53: applet.start();
54: frame.setVisible(true);
55: }
56:
57: public static void run(JPanel panel, int width, int height) {
58: JFrame frame = new JFrame(title(panel));
59: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
60: frame.getContentPane().add(panel);
61: frame.setSize(width, height);
62: frame.setVisible(true);
63: }
64:
65: public static void setNativeLookAndFeel() {
66: try {
67: UIManager.setLookAndFeel(UIManager
68: .getSystemLookAndFeelClassName());
69: } catch (Exception e) {
70: System.out.println("Error setting native LAF: " + e);
71: }
72: }
73:
74: public static void setJavaLookAndFeel() {
75: try {
76: UIManager.setLookAndFeel(UIManager
77: .getCrossPlatformLookAndFeelClassName());
78: } catch (Exception e) {
79: System.out.println("Error setting Java LAF: " + e);
80: }
81: }
82:
83: public static void setMotifLookAndFeel() {
84: try {
85: UIManager
86: .setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
87: } catch (Exception e) {
88: System.out.println("Error setting Motif LAF: " + e);
89: }
90: }
91:
92: public static void centerWindow(Window window) {
93: Dimension dim = window.getToolkit().getScreenSize();
94: window.setLocation(dim.width / 2 - window.getWidth() / 2,
95: dim.height / 2 - window.getHeight() / 2);
96: }
97: }
|