Java中的LookAndFeel

Java'中的几种Look and Feel
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包中

参考代码如下:
 1 ExpandedBlockStart.gif ContractedBlock.gif public void actionPerformed(ActionEvent e) {
 2InBlock.gif    String lnfName = null;
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    if (e.getActionCommand().equals("Metal")) dot.gif{
 4InBlock.gif        lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else if (e.getActionCommand().equals("Motif")) dot.gif{
 6InBlock.gif        lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else dot.gif{
 8InBlock.gif        lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
 9ExpandedSubBlockEnd.gif    }

10ExpandedSubBlockStart.gifContractedSubBlock.gif    try dot.gif{
11InBlock.gif        UIManager.setLookAndFeel(lnfName);
12InBlock.gif        SwingUtilities.updateComponentTreeUI(frame);
13ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (UnsupportedLookAndFeelException ex1) dot.gif{
14InBlock.gif        System.err.println("Unsupported LookAndFeel: " + lnfName);
15ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (ClassNotFoundException ex2) dot.gif{
16InBlock.gif        System.err.println("LookAndFeel class not found: " + lnfName);
17ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (InstantiationException ex3) dot.gif{
18InBlock.gif        System.err.println("Could not load LookAndFeel: " + lnfName);
19ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (IllegalAccessException ex4) dot.gif{
20InBlock.gif        System.err.println("Cannot use LookAndFeel: " + lnfName);
21ExpandedSubBlockEnd.gif    }

22ExpandedBlockEnd.gif}
dot.gif

转载于:https://www.cnblogs.com/xxpal/articles/1232472.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值