2012-03-15 44 views
3

嗨我有一個小菜單的Swing應用程序。 創建了包含menues文本的前兩個屬性,然後將lookandfeel設置爲windows,最後填充menues。 這裏是源代碼:JMenu沒有在Windows的外觀和感覺

private JMenu[] Menue={new JMenu("File")}; 

private JMenuItem[][] MenuItemsString ={{new JMenuItem("Import"),new JMenuItem("Export")}}; 
... 
public window(){ 
     super ("Q3MeshConverter"); 

     plate = new JPanel(new BorderLayout()); 
     try{ 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// set Windows lookandfeel 
      } 
      catch(Exception x){ 

      } 
     menuBar = new JMenuBar(); 
     ... 
     setJMenuBar(menuBar); 

     JMenu[] Menu =Menue; 
     JMenuItem[][] MenuItems =MenuItemsString; 
     for(int m=0;m<Menu.length;m++){// loop trough the Menu(es) 
      menuBar.add(Menu[m]); 
      for(int mi=0;mi<MenuItems[m].length;mi++){// loop through the MenuItems 
       Menu[m].add(MenuItems[m][mi]); 
       MenuItems[m][mi].addActionListener(this); 
      } 
     } 
     ... 
     setContentPane (plate); 
} 

這就是醜陋的輸出:

爲什麼它看起來像這樣?

+0

您是否嘗試過檢查異常而不是忽略它以確保l&f實際上已被更改:'catch(Exception x){System.out.println(「Could not change the look and feel」);}' – assylias 2012-03-15 12:30:10

+0

你,但我已經檢查過它,並沒有例外。 – 2012-03-15 12:34:30

+0

如果將'setLookAndFeel(...)'更改爲'setLookAndFeel(UIManager.getSystemLookAndFeelClassName())',那麼有什麼更改? – 2012-03-15 12:39:55

回答

4

設置一下,在你main方法感到:

public static void main(String[] args) { 
    try { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } catch (Exception e) { } 
} 
+0

是的,這是解決問題最簡單的方法;只是把它放在主要方法內的任何代碼之前。 – ecle 2012-03-15 12:57:59

8

有沒有神奇的LAF變化之前創建的組件如何能夠了解它,你必須告訴它:-)

SwingUtilities.updateComponentTreeUI(someComponent);