您的位置:首页 > Web前端

Swing 观感设置(LookAndFeel)(转)

2015-04-24 08:19 495 查看
 Swing通过以下方式实现观感:将JComponent分离成两个独立的类,一个JComponent子类,一个ComponentUI子类。ComponentUI子类在swing中有很多叫法:"the UI," "component UI," "UI delegate," "look and feel delegate"。

   每个L&F都必须为ComponentUI提供具体实现的子类。

  1、可获得的L&F

  (1)CrossPlatformLookAndFeel:Java l&f,即在各个平台上的效果相同,这是默认的观感,是JAVA API javax.swing.plaf.metal的一部分。

  (2)SystemLookAndFeel:观感依赖于本地系统。

  (3)Synth:使用XML自定义观感。

  (4)Multiplexing:同时采用多个观感。

  SystemL&F 在以下包中:

  com.sun.java.swing.plaf.gtk.GTKLookAndFeel

  com.sun.java.swing.plaf.motif.MotifLookAndFeel

  com.sun.java.swing.plaf.windows.WindowsLookAndFeel

 2、通过程序设置观感

 设置观感最好在应用的第一步完成,通过UIManager来设置L&F,

UIManager.setLookAndFeel(

            UIManager.getCrossPlatformLookAndFeelClassName());

UIManager.setLookAndFeel(

            UIManager.getSystemLookAndFeelClassName());

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

 3、通过命令行设置观感

 java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel MyApp

 java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp

 4、通过swing.properties 设置观感

 # Swing properties

 swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

 5、如何选择观感

 (1)程序通过setL&F方法显示设置观感;

 (2)如无则使用swing.defaultlaf定义的观感;如同时在命令行中设置观感,命令行优先;

 (3)最后,使用JAVA L&F。

 6、修改观感

 但应用GUI已经显示后,仍然可以修改观感。

 UIManager.setLookAndFeel(lnfName);

SwingUtilities.updateComponentTreeUI(frame);

frame.pack();

7、主题(Theme)

主题可以用来快速改变组件上文字的风格和颜色。每个L&F都有自己独立的主题:

 if (LOOKANDFEEL.equals("Metal")) {

    if (THEME.equals("DefaultMetal"))

       MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());

    else if (THEME.equals("Ocean"))

       MetalLookAndFeel.setCurrentTheme(new OceanTheme());

    else

       MetalLookAndFeel.setCurrentTheme(new TestTheme());                    

    UIManager.setLookAndFeel(new MetalLookAndFeel());

 } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swing LookAndFeel