如何设置Java桌面程序界面外观(How to Set the Look and Feel)
2011-09-12 14:25
651 查看
内容目录:
1. 使用UIManager.setLookAndFeel通过编程来指定界面外观
2. UI Manager如何选择界面外观?
3. 启动后, 改变界面外观
1. 使用UIManager.setLookAndFeel通过编程来指定界面外观.
示例代码如下:][1]
方法setLookAndFeel中的参数是类LookAndFeel的子类的全称(fully qualified name). 要设置Java界面外观(Java Look and Feel), 我们使用getCrossPlatformLookAndFeelClassName方法. 如果你想设置为程序所运行的平台的界面外观, 可以使用getSystemLookAndFeelClassName方法.
要设置特殊的UI, 你可以使用具体的类名. 例如你想设置Windows界面外观, 可以使用下面的代码:
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'文件内容的例子:
3) 如果程序或用户都没有成功设置界面外观, 则程序使用Java的界面外观(译者注:即"javax.swing.plaf.metal.MetalLookAndFeel").
3. 启动后, 改变界面外观
即使程序的GUI显示出来后, 也可以通过setLookAndFeel方法来改变界面外观. 要使已有的组件显示新的界面外观, 在每个顶层容器(top-level container)(译者注:顶层容器, 例如在Swing中一般为JFrame, JApplet, JDialog或其子类的对象)调用类SwingUtilities的updateComponentTreeUI方法, 然后你可能希望重置每个顶层容器的大小来适应它所包含的组件的新大小,
例如:
注:
此文章翻译自《The Java Tutorial》的离线文档'Creating a GUI with JFC/Swing'中的'How to Set the Look and Feel'
[1]为了展示一个完整的可运行的程序, 我对原文的代码进行了更改, 原代码如下:
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");下面是方法setLookAndFeel的一些参数:
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打造绚丽的界面外观!
- How to see the Master Page option Under Look and Feel in site settings?
- How to get and set the drawing order of layers in globe(获取并设置Globe图层的叠加次序:)
- 【转】[jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
- 如何给上传控件上色【HOW TO SET CSS FOR THE CONTROL OF FILEUPLOAD】
- How to add a user and set environments for the us
- Java: 如何加密解密 How to encrypt and decrypt
- Look and Feel---JAVA界面(换肤术)
- How To Get Application Path,And How To Get The Path's Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息)
- how to set ImageView fillet 如何设置ImageView为圆角(iOS)
- How to set globe layer cache properties(如何设置globe图层的缓存属性)
- A Visual Guide to Swing Components (Java Look and Feel)
- How to change the language to English for Oracle SQL Developer(如何将SQL developer改成英文界面)