2012-11-08 31 views
2

我的JMenuBar沒有顯示出來,當我跑我的應用程序。 我該如何解決這個問題?我JMenu的酒吧沒有顯示出來

所以,當我跑我的JFrame我要看看我在上面的JMenuBar。

我的佈局是

代碼:

package view; 

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.WindowConstants; 


public class Home extends JFrame { 

    private Container window = getContentPane(); 

    public Home(){ 
     initGUI(); 
    } 

    public void initGUI(){ 

     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   
     setPreferredSize(new Dimension(800, 600)); 
     setLayout(null); 

     JMenuBar menu = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     menu.add(file); 
     window.add(menu); 



     pack(); 

    } 
    } 
+1

無關,但重要的是:永不永遠做任何手工上漿/定位(又名:空佈局),不要忘了閱讀和理解中引用的教程swing標籤維基 - 它會回答這樣的基本問題:-) – kleopatra

回答

3
  • 您沒有添加JMenuBarJFrame

  • 使用setJMenuBar(menu);

+0

但我做到了:window.add(menu);在這種情況下, – user1809035

+0

JMenuBar不返回PreferredSize,那麼它的大小是0,0 – mKorbel

1

而不是相加(菜單)的調用

  • setJMenuBar(菜單);

而且您更好地使用SwingUtilities.invokeLater()