1 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 26 public class JFrameUtilities { 27 28 31 public static String title(Object o) { 32 String t = o.getClass().toString(); 33 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.getSystemLookAndFeelClassName()); 68 } catch(Exception e) { 69 System.out.println("Error setting native LAF: " + e); 70 } 71 } 72 73 public static void setJavaLookAndFeel() { 74 try { 75 UIManager.setLookAndFeel 76 (UIManager.getCrossPlatformLookAndFeelClassName()); 77 } catch(Exception e) { 78 System.out.println("Error setting Java LAF: " + e); 79 } 80 } 81 82 public static void setMotifLookAndFeel() { 83 try { 84 UIManager.setLookAndFeel 85 ("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 86 } catch(Exception e) { 87 System.out.println("Error setting Motif LAF: " + e); 88 } 89 } 90 91 public static void centerWindow(Window window) { 92 Dimension dim = window.getToolkit().getScreenSize(); 93 window.setLocation(dim.width/2 - window.getWidth()/2, 94 dim.height/2 - window.getHeight()/2); 95 } 96 } 97 | Popular Tags |