Java中改变应用程序界面外观(javax.swing.UIManager类和LookAndFeel类)

时间:2022-06-29 10:58:29

本文转自:披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、若是改变整个应用程序的观感,则代码为:
Java中改变应用程序界面外观(javax.swing.UIManager类和LookAndFeel类)

2、若是改变某个面板的观感,则代码为:
UIManager.setLookAndFeel(观感名);
SwingUtilities.updateComponentTreeUI(面板引用);
Java中改变应用程序界面外观(javax.swing.UIManager类和LookAndFeel类)以上代码必须能够被捕获异常,否则报错。
Java中改变应用程序界面外观(javax.swing.UIManager类和LookAndFeel类)

非常感谢原文作者,亲测有效!