import javax.swing.*; public class F extends JFrame { JFrame f=new JFrame();
f.setDefaultLookAndFeelDecorated(true);
F() { f.setSize(300,200); f.setDefaultCloseOperation(3); f.setVisible(true); } public static void main(String[] args) { new F(); } }
如何不繼承jframe;
import javax.swing.*; public class F{ JFrame f = new JFrame(); F() { f.setSize(300, 200); f.setDefaultCloseOperation(3); f.setVisible(true); } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); //調用 new F(); } }
必須要先用JFrame.setDefaultLookAndFeelDecorated(true);設定了後,再産生JFrame的對這樣才能生效
如果先産生了對象在調用JFrame.setDefaultLookAndFeelDecorated(true);方法 會不能生效
設定Look and Feel
Swing允許在程式中指定Look and Feel,即:Java look and feel,Windows look and feel和CDE/Motif look and feel等。可以使用UIManaer.setLookAndFeel方法指定Look and Feel,在下面的代碼段中,使用getCrossPlatformLookAndFeelClassName方法得到跨平台的Look and Feel(即Java look and feel).
public static void main(String[]args) { try{ UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); }catch(Exception e) { } …//Create and show the GUI… }
如果希望使用程式目前運作的平台所用的Look and Feel,可以使用UIManager.getSystem LookAndFeelClassName方法。當然,還可以直接使用實際的類名來指定,例如:
UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.windows.windowsLookAndFeel”);
總的來說,對于UIManager.setLookAndFeel方法,可以使用下面的參數來設定look and feel:
★UIManager.getCrossPlatformLookAndFeelClassName()
傳回一個在任何平台都肯定能正常工作的Look and Feel,即Java look and feel.
★UIManager.getSystemLookAndFeelClassName()
指定目前平台所使用的Look and Feel。在Win32平台,則指定為Windows lood and feel;在Mac OS平台,為Mac OS look and feel;在Sun平台,為CDE/Motiflook and feel.
★“javax.swing.plaf.metal.MetalLookAndFeel”
指定為Java look and feel,它與UIManager.getCrossPlatformLookAndFeelClassName()方法相同。
★“com.sun.java.swing.plaf.windows.Windows.WindosLookAndFeel”
指定為CDE/Motif look and feel,可以在任何平台上使用。
★“java.swing.plat.mac.MacLookAndFeel”
指定為Mac OS look and feel,它公适用Mac OS平台。
當然,還可以使用其他參數,執行在UIManager.aetLookAndFeel方法中指定相應的類名。
在設定了Look and feel之後,還可以使用下面的代碼來改變Look and Feel:
UIManager.setLookAndFeel(InfName);
SwingUtilities.rpdateComponentTreeUI(frame);
Frame.pack();
如果每次都使用固定的Look and Feel,還可以通過在程式之外設定環境變量來實作。在J2SE的HOME目錄(例如c:\jdk1.3)下,找到lib子目錄,建立一個檔案swing.properties(如果不存在),在檔案中加入類似于下面的内容:
#Swing properties
swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
使用 javax.swing.UIManager 可以更改界面風格, 應該在建立畫面之前執行,否則設定有可能不起作用. 在 GUI 顯示後,也可以改變界面風格,但可見元件不會自動更新顯示外觀,需要對每個頂層容器調用 SwingUtilities.updateComponentTreeUI() 方法.
主要的 Look & Feel 有: Java/Metal, Windows, CDE/Motif, 但如果有其它的 L&F 包,還可以擴充.隻要指定類名加載即可.
設定語句:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // 目前系統預設 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); // 跨平台 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // Windows
免費的laf :Napkin http://napkinlaf.sourceforge.net/