要改变Swing默认的LookAndFeel,网上都说用UIManager下的一个静态方法setLookAndFeel即可,但是我用了这个方法有半年的时间也没有看到真正的WindowsLookAndFeel。昨天网上无意中才看到正解,要设置LookAndFeel,不仅要调用上面提到的方法,还要调用一个SwingUtilities类中的静态方法updateComponentTreeUI。即
try{
javax.swing.UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}catch(javax.swing.UnsupportedLookAndFeelException e){
e.printStackTrace();
}
后者在运行时对整个ComponentTree进行更新,应用当前的UI设置。
public static void setLookAndFeel(String className, java.awt.Component c) {
try {
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(c);//注意这行
}
catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null,
"不好意思,setLookAndFeel时出错了:( Errormsg:" + ex,
"setLookAndFeel",
JOptionPane.INFORMATION_MESSAGE);
}
|