晓洛的茶园  
犀利~各种犀利
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 11
  • 文章 - 0
  • 评论 - 0
  • 引用 - 0

导航

常用链接

留言簿(1)

随笔分类

随笔档案

个人链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 




 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());
 } 

posted on 2009-12-08 14:08 晓洛 阅读(3145) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 晓洛 Powered by: 博客园 模板提供:沪江博客