要改變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);
}
|