2012-03-02 58 views
8

我就在哪裏把這樣的困惑:如何設置JFrame的外觀和感覺

try { 
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
} catch(Exception e){ 

} 

我並沒有延續JFrame類,但使用JFrame f = new JFrame(); 感謝:d

+0

確保Look'n'Feel配置** **前初始化框架。 – 2012-03-02 23:34:47

+0

[編程設置外觀和感覺](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#programmatic) – chicout 2012-03-02 22:11:46

回答

6

最常見的地方把它放在你的static void main(String [] args)方法裏面。 像這樣:

public static void main(String[] args) 
{ 
    try 
    { 
     UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
    } 
    catch(Exception e){ 
    } 
    new YourClass(); //start your application 
} 

更多信息看看這個網站: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

+2

基本上是正確的,但不推薦Nimbus :)它開始了它的生活com.sun。*在jdk6中,肯定會被移入jdk7中的javax.swing。因此,而不是硬編碼的類名,查詢UIManager的安裝lookAndFeels,並通過它們循環,直到包含「Nimbus」的類被發現 – kleopatra 2012-03-13 11:29:51

+0

老實說,我從來沒有使用任何外觀和感覺我的Java程序。但是,如果我永遠不會,我會用你的片段!非常感謝 – 2012-03-13 12:32:42

9

注:這不是一個問題的答案(這是其中設置LAF)。相反,它回答了問題如何以獨立於其包名稱的方式設置LAF。如班級搬遷,簡化生活,如f.i.從com.sun *到javax.swing的Nimbus。

基本的方法是查詢UIManager安裝的LAF,循環遍歷它們直到找到匹配並設置它。 Here'r這樣的方法在SwingX實施:

/** 
* Returns the class name of the installed LookAndFeel with a name 
* containing the name snippet or null if none found. 
* 
* @param nameSnippet a snippet contained in the Laf's name 
* @return the class name if installed, or null 
*/ 
public static String getLookAndFeelClassName(String nameSnippet) { 
    LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels(); 
    for (LookAndFeelInfo info : plafs) { 
     if (info.getName().contains(nameSnippet)) { 
      return info.getClassName(); 
     } 
    } 
    return null; 
} 

使用(這裏沒有異常處理)

String className = getLookAndFeelClassName("Nimbus"); 
UIManager.setLookAndFeel(className); 
8

UIManager.setLookAndFeel()不會對已創建的組件協同工作。這是爲應用程序中的每個窗口設置外觀和感覺的好方法。這會將其設置在您程序中的所有打開的Windows上。任何創建的新窗口都將使用UIManager設置的內容。

UIManager.setLookAndFeel(lookModel.getLookAndFeels().get(getLookAndFeel())); 
    for(Window window : JFrame.getWindows()) { 
     SwingUtilities.updateComponentTreeUI(window); 
    } 
2

在創建JFrame之後,或者在擴展JFrame的類的構造函數中,可以將此塊放入主方法中。

 

    try 
    { 
     //Set the required look and feel 
     UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     //Update the component tree - associate the look and feel with the given frame. 
     SwingUtilities.updateComponentTreeUI(frame); 
    }//end try 
    catch(Exception ex) 
    { 
     ex.printStackTrace(); 
    }//end catch 
 
0
try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException | InstantiationException || javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger( Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    }