Java中改變應用程序界面外觀(javax.swing.UIManager類和LookAndFeel類)

本文轉自:披Zhe羊皮De狼,原文地址:http://blog.csdn.net/u010995220/article/details/49847307

  • javax.swing.UIManager類
javax.swing.UIManager類是Swing界面管理核心,管理Swing應用程序樣式。
  • LookAndFeel抽象類
與javax.swing.UIManager類密切相關的就是LookAndFeel抽象類。它除了提供static方法,還定義抽象的個性化設置方法由子類實現。
Sun提供了三個LookAndFeel子類:javax.swing.plaf.metal.MetalLookAndFeel(Metal樣式)、com.sun.java.swing.plaf.motif.MotifLookAndFeel(Motif樣式)、com.sun.java.swing.plaf.windows.WindowsLookAndFeel(Windows樣式)。
  • Java中的幾種LookandFeel(此部分代碼在main方法打開GUI界面之前實現)
1、Metal風格(默認)
String lookAndFeel ="javax.swing.plaf.metal.MetalLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);

2、Windows風格
String lookAndFeel ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);

3、Windows Classic風格
String lookAndFeel ="com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);

4、Motif風格
String lookAndFeel ="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);

5、Mac風格 (需要在相關的操作系統上方可實現)
String lookAndFeel ="com.sun.java.swing.plaf.mac.MacLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);

6、GTK風格 (需要在相關的操作系統上方可實現)
String lookAndFeel ="com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
7、可跨平臺的默認風格
String lookAndFeel =UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);

8、當前系統的風格
String lookAndFeel =UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
  • 一個實例
1、若是改變整個應用程序的觀感,則代碼爲:


2、若是改變某個面板的觀感,則代碼爲:
UIManager.setLookAndFeel(觀感名);
SwingUtilities.updateComponentTreeUI(面板引用);
以上代碼必須能夠被捕獲異常,否則報錯。


非常感謝原文作者,親測有效!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章