如何设置Java桌面程序界面外观(How to Set the Look and Feel)
来源:互联网 发布:淘宝优惠券发单软件 编辑:程序博客网 时间:2024/05/16 12:01
内容目录:
1. 使用UIManager.setLookAndFeel通过编程来指定界面外观
2. UI Manager如何选择界面外观?
3. 启动后, 改变界面外观
1. 使用UIManager.setLookAndFeel通过编程来指定界面外观.
示例代码如下:[1]
- import javax.swing.*;
- import java.awt.*;
- public class Hello extends JFrame {
- public Hello() {
- setPreferredSize(new Dimension(600, 400));
- pack();
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) {
- try {
- String lookAndFeel =
- UIManager.getCrossPlatformLookAndFeelClassName();
- UIManager.setLookAndFeel(lookAndFeel);
- } catch (Exception e) {}
- new Hello();
- }
- }
方法setLookAndFeel中的参数是类LookAndFeel的子类的全称(fully qualified name). 要设置Java界面外观(Java Look and Feel), 我们使用getCrossPlatformLookAndFeelClassName方法. 如果你想设置为程序所运行的平台的界面外观, 可以使用getSystemLookAndFeelClassName方法. 要设置特殊的UI, 你可以使用具体的类名. 例如你想设置Windows界面外观, 可以使用下面的代码:
- UIManager.setLookAndFeel(
- "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.getCrossPlatformLookAndFeelClassName()
--Java的界面外观, 所有平台均一致.
UIManager.getSystemLookAndFeelClassName()
--指定为当前平台的界面外观.在32位Windows平台, 为Windows界面外观; 在Mac OS平台, 为Mac OS界面外观; 在Sun平台, 为CDE/Motif界面外观.
"javax.swing.plaf.metal.MetalLookAndFeel"
--指定为Java的界面外观, 也就是方法UIManager.getCrossPlatformLookAndFeelClassName()的返回值.
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
--指定为Windows的界面外观, 仅在Windows平台起作用.
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"
--指定为CDE/Motif的界面外观, 可以在所有平台起作用.
"javax.swing.plaf.mac.MacLookAndFeel"
--指定为Mac OS的界面外观, 仅在Mac OS平台起作用.
2. UI Manager如何选择界面外观?
1) 如果程序在创建所有组件之前设置了界面外观(Look and feel), UI manager则尝试创建指定界面外观类的实例(an instance of the specified look-and-feel class), 如果成功, 所有的组件均使用这种界面外观.
2) 如果程序没有成功设置界面外观, 则UI manager在创建第一个组成的UI之间, 检测用户是否在名为'swing.properties'的文件中指定了界面外观. 在Java版本的lib目录下搜索这个文件. 例如, 如果你的Java解释器(Java interpreter)在javaHomeDirectory\bin目录下, 则'swing.properties'文件(如果存在)应该在javaHomeDirectory\lib目录下. 如果用户指定了一个界面外观(译者注:即文件'swing.properties'中的配置), 则UI manager再次尝试实例化界面外观类. 下面是一个'swing.properties'文件内容的例子:
- # Swing properties
- swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
3) 如果程序或用户都没有成功设置界面外观, 则程序使用Java的界面外观(译者注:即"javax.swing.plaf.metal.MetalLookAndFeel").
3. 启动后, 改变界面外观
即使程序的GUI显示出来后, 也可以通过setLookAndFeel方法来改变界面外观. 要使已有的组件显示新的界面外观, 在每个顶层容器(top-level container)(译者注:顶层容器, 例如在Swing中一般为JFrame, JApplet, JDialog或其子类的对象)调用类SwingUtilities的updateComponentTreeUI方法, 然后你可能希望重置每个顶层容器的大小来适应它所包含的组件的新大小, 例如:
- UIManager.setLookAndFeel(lnfName);
- SwingUtilities.updateComponentTreeUI(frame);
- frame.pack();
注:
此文章翻译自《The Java Tutorial》的离线文档'Creating a GUI with JFC/Swing'中的'How to Set the Look and Feel'
[1]为了展示一个完整的可运行的程序, 我对原文的代码进行了更改, 原代码如下:
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(
- UIManager.getCrossPlatformLookAndFeelClassName());
- } catch (Exception e) { }
- new SwingApplication(); //Create and show the GUI.
- }
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel) UIManager
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- 关于JAVA界面风格(How to Set the Look and Feel)
- Java 用Look And Feel打造绚丽的界面外观!
- 示例:界面外观Examples:Look And Feel
- Look and Feel---JAVA界面(换肤术)
- Look and Feel---JAVA界面(换肤术)
- Look and Feel---JAVA界面(换肤术)
- Look and Feel---JAVA界面(换肤术)
- Look and Feel---JAVA界面(换肤术)
- How to see the Master Page option Under Look and Feel in site settings?
- [Java Swing]如何设置Look & Feel
- A Visual Guide to Swing Components (Java Look and Feel)
- 如何改变netbeans的look and feel
- 关闭tp引号自动注释
- 美国老师教画画没样板[图]
- 个人电脑搭建服务器
- spring中context:property-placeholder/元素
- java 多线程 wait() 以及 notirfy() 简析
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- oracle实例坏掉的一种可能
- eclipse如何修改dynamic web module version
- Linux设备模型(7)_Class
- 关于Windiws XP 中安装IIS 5.0的步骤以及遇到问题的解决方案
- [2011山东省第二届ACM大学生程序设计竞赛]——Crack Mathmen
- zookeeper中Watcher和Notifications
- LR(1) 分析例子
- Java Web开发 之HTTP状态码整理