2016-12-02 83 views
0

我的menuBar没有显示。我是否需要JPanel才能在我的GUI中显示?菜单栏GUI应用程序

private void buildCtrlPanel() { 
     ctrlPanel = new JPanel(); 
     menuBar = new JMenuBar(); 
     fileMenu = new JMenu("File"); 
     optionsMenu = new JMenu("Options"); 

     JFrame frame = new JFrame(); 
     frame.setJMenuBar(menuBar); 
     frame.setSize(350, 250); 
     frame.setVisible(true); 

     ctrlPanel.setLayout(new FlowLayout()); 
     ctrlPanel.add(menuBar); 
     ctrlPanel.add(frame); 
     menuBar.add(fileMenu); 
     menuBar.add(optionsMenu); 
    } 

回答

1

只能添加一个组件一个容器。您已经将JMenuBar正确地添加到了JFrame中 - 很好,但是您还将它错误地添加到了使用FlowLayout的JPanel(为什么?),布局不适合JMenuBars(又是为什么?)。解决方案:不要这样做。像你已经做的那样将它添加到JFrame中,并保留它。

你似乎也在为JPanel添加一个JFrame - 你不应该这样做,并且再次表明你将在继续之前通过Swing教程。

  • 你可以找到链接到秋千教程和其他Swing资源在这里:Swing Info
  • Swing的菜单教程可以在这里找到:How to use Menus
相关问题