2012-02-23 125 views
1

線程標題已經解釋了我的問題。這是一個已知的錯誤?我搜索了互聯網,但找不到解決方案。JMenuBar不顯示在Mac OS X Lion上,但在Win7上顯示

那麼,你可能知道該怎麼辦?

public static void main(String[] args) { 
    JFrame frame = new JFrame("Menu"); 
    frame.setVisible(true); 
    frame.setSize(500,500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JMenuBar menubar = new JMenuBar(); 
    frame.setJMenuBar(menubar); 

    JMenu file = new JMenu("File"); 
    menubar.add(file); 
    JMenuItem exit = new JMenuItem("Exit"); 
    file.add(exit); 

    JMenu help = new JMenu("Help"); 
    menubar.add(help); 
    JMenuItem about = new JMenuItem("About"); 
    help.add(about); 

class exitAction implements ActionListener { 

    public void actionPerformed(ActionEvent e){ 
     System.exit(0); 
    } 
} 

exit.addActionListener(new exitAction()); 
} 
+0

嘗試在添加完所有元素後設置菜單欄。 – 2012-02-23 17:27:47

回答

2

1)你的代碼行

frame.setVisible(true); 

必須在main method

2)Swing GUI最後行代碼不是線程安全的,那麼main method應該被包裝成invokeLater()

+0

謝謝。這工作! – Baloo 2012-02-23 17:34:09