經常聽朋友說,java編寫的程序界面比較單一,不好進行個性化配置?,F在讓我們一起來了解有關Java界面樣式相關類的知識,以及如何用Java寫出變幻莫測的用戶界面,讓Java程序也擁有時髦的換膚功能。 實現原理 Java平臺成熟后,設計人員與開發人員就熟悉到需要連續性好、兼容性好、輕易使用的Java程序界面。這時Sun就推出了“Look and Feel”機制迎合這種需求。它提供了一種獨特的、與平臺無關的程序外觀,以及標準的界面行為。它可以在各個平臺上使用同一“Look and Feel”,從而縮短設計與開發周期,降低軟件使用人員的培訓費用。這是“Look and Feel”設計的初衷?,F在我們來使用這種特性為Java程序穿上花衣。
從JDK1.1.3開始,Sun提供了三個LookAndFeel的子類 javax.swing.plaf.metal.MetalLookAndFeel、com.sun.java.swing.plaf.motif.MotifLookAndFeel、com.sun.java.swing.plaf.windows. WindowsLookAndFeel。它們分別提供了“Metal”、“Motif”與“Windows”的界面式樣。也就是說,任何基于Swing的界面程序本身都可以使用三種系統提供的皮膚。實際上我們也可以直接或間接繼續LookAndFeel類,自己編寫一種“皮膚”。在這里我們要使用到一個開放源代碼的產品SKIN Look And Feel 1.2.2,在http://www.l2fPRod.com/可以找到它的全部源代碼。Skin Look And Feel本身還可以更換http://www.l2fprod.com/提供的各種“皮膚”,讓你的程序可以各種“皮膚”示人。
UIManager類
這個類就是Swing界面治理的核心,治理Swing的小應用程序以及應用程序樣式的狀態。UIManager類提供了下列靜態方法用于更換與治理“Look and Feel”:
static void addAuxiliaryLookAndFeel(LookAndFeel laf) //增加一個“Look And Feel”到輔助的“look and feels”列表 static LookAndFeel[] getAuxiliaryLookAndFeels() //返回輔助的“look and feels”列表(可能為空)。 static String getCrossPlatformLookAndFeelClassName() //返回缺省的實現了跨平臺的Look and Feel——即Java Look and Feel(JLF)。 static UIManager.LookAndFeelInfo[] getInstalledLookAndFeels() //返回了在目前已經安裝的LookAndFeel的信息。 static LookAndFeel getLookAndFeel() //返回當前使用的Look and Feel static String getSystemLookAndFeelClassName() //返回與當前系統相關的本地系統Look and Feel,假如沒有實現本地Look and Feel則返回缺省的跨平臺的Look and Feel。 static void installLookAndFeel(String name, String className) //創建一個新的Look and Feel并安裝到當前系統。 static void installLookAndFeel(UIManager.LookAndFeelInfo info) //創建一個新的Look and Feel并安裝到當前系統。 static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf) //從輔助的“look and feels”列表刪除一個“Look And Feel” static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos) //設置當前的已安裝Look and Feel信。 static void setLookAndFeel(LookAndFeel newLookAndFeel) //設置當前使用的LookAndFeel。 static void setLookAndFeel(String className) //設置當前使用的LookAndFeel。參數是類名。
下面的源代碼可以在Skin Look And Feel 1.2.2下的源代碼根目錄下找到(比如我下載的zip包是skinlf-1.2.2-20020611.zip,解壓后,在src目錄下的Skinit.java)。
public class Skinit extends javax.swing.JApplet { /** * The main program for the Skinit class * * @param args The command line arguments * @exception Exception Description of Exception */ public static void main(String[] args) throws Exception { if (args.length == 0) { printUsage(); } int mainClassNameIndex = -1; String gtktheme = null; String kdetheme = null; String packtheme = null;
for (int i = 0, c = args.length; i < c; i++) { if (args[i].equals("-gtk")) { gtktheme = args[++i]; } else if (args[i].equals("-kde")) { kdetheme = args[++i]; } else if (args[i].equals("-pack")) { packtheme = args[++i]; } else { mainClassNameIndex = i; break; } }
String[] realArgs = new String[args.length - mainClassNameIndex - 1]; for (int i = 0, c = realArgs.length; i < c; i++) { realArgs[i] = args[mainClassNameIndex + i + 1]; }
// First try to find the class Class clazz = null; try { clazz = Class.forName(args[mainClassNameIndex]); } catch (ClassNotFoundException e) { System.err.println("The class " + args[mainClassNameIndex] + " was not found in the classpath."); System.exit(1); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } // if the class exists, get the main method Method mainMethod = null; try { mainMethod = clazz.getMethod("main", new Class[]{String[].class}); } catch (NoSUChMethodException e) { System.err.println("No method public static void main(String[] args) in " + clazz.getName()); System.exit(1); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } // try to make sure the main method is accessible try { mainMethod.setAccessible(true); } catch (Throwable e) { } // main class and main method found, time to load the skin Skin skin = null; if (packtheme != null) { if (SkinUtils.DEBUG) { System.out.println("Loading themepack " + packtheme); } skin = SkinLookAndFeel.loadThemePack(packtheme); } else if (gtktheme != null) { if (kdetheme != null) { skin = new CompoundSkin(SkinLookAndFeel.loadSkin(gtktheme), SkinLookAndFeel.loadSkin(kdetheme)); } else { skin = SkinLookAndFeel.loadSkin(gtktheme); } } /* * try to use the user default skin */ if (skin == null) { if (SkinUtils.DEBUG) { System.out.println("Trying user skin"); } skin = SkinLookAndFeel.getSkin(); } if (skin != null) { SkinLookAndFeel.setSkin(skin); SkinLookAndFeel lnf = new SkinLookAndFeel(); UIManager.setLookAndFeel(lnf); UIManager.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { Object newLF = event.getNewValue(); if ((newLF instanceof SkinLookAndFeel) == false) { try { UIManager.setLookAndFeel(new SkinLookAndFeel()); } catch (Exception e) { e.printStackTrace(); } } } }); } else { System.out.println("No GTK theme provided, defaulting to application Look And Feel"); } try { mainMethod.invoke(null, new Object[]{realArgs}); } catch (IllegalAccessException e) { System.err.println("Please make sure the class " + clazz.getName() + " and the method main(String[] args) are public."); System.exit(1); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } /** * Description of the Method */ static void printUsage() { String usage = "Skinit - Skin Look And Feel wrapper/n" + "Usage: skinit [options] class [args...]/n" + "