关于LookAndFeel

约翰羊 2011-01-26 09:03:49
我想在一个界面中,JPanel使用java默认的LookAndFeel,而JButton使用window的LookAndFeel
就是在一个界面,让不同的控件显示不同的风格。
应该怎么做?谢谢。
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
TKD03072010 2011-01-26
  • 打赏
  • 举报
回复
在默认情况下,Swing程序使用Metal观感,可以采用两种方式改变观感。第一种方式是在Java安装的子目录jre/lib下有一个文件swing.properties。在这个文件中,将属性swing.defaultlaf设置为所希望的观感类名。例如,swing.defaultlaf = com.sun.java.swing.plaf.motif.MotifLookAndFeel
第二种方式是动态地改变观感。这需要调用静态的UIManager.setLookAndFeel方法,并提供所想要的观感类名,然后再调用静态方法SwingUtilities.updateComponentTreeUI来刷新全部的组件集。
String plaf="com.sun.java.swing.plaf.motif.MotifLookAndFeel"
try{
UIMananger.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(panel);
{
catch(Exception e) {e.printStackTrace();}
  • 打赏
  • 举报
回复
Java中的LookAndFeel 详解

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

  在Java中让用户能够动态地更改应用的外观,可以给用户更好地体验,具体的实现方式是:

  1,先使用UIManager.setLookAndFeel(String s)方法设定对应的外观

  2,再使用SwingUtilities.updateComponentTreeUI(Component c)方法立刻更新应用的外观

  这两个类均在javax.swing包中

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧