【问题标题】:JFrame doesn't displayed the JMenuBar in IntellijJFrame 未在 Intellij 中显示 JMenuBar
【发布时间】:2016-01-25 14:47:00
【问题描述】:

当我运行程序时,我看到一个空白窗口。 我该如何解决这个问题?谢谢。我做错了什么? 这是我的代码:

public class Environment{
    private JFrame frame;
    private JMenu jmenu;
    private JMenuItem menuItem;
    private JMenuBar menuBar;

    Environment(){

    frame = new JFrame("Notepad");

        menuBar = new JMenuBar();
        //menuBar.setVisible(true);
        jmenu = new JMenu("Test");
        menuItem = new JMenuItem("Open");

        jmenu.add(menuItem);


        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(660, 350);

            // Set a main menu
            frame.setJMenuBar(menuBar);
            menuBar.add(jmenu);

    frame.setVisible(true);
}
}

【问题讨论】:

  • 菜单显示正确,你的框架是空的,因为你没有在里面添加任何组件。
  • @NarutoBijuMode 你好。像按钮、标签等?
  • 是的,框架是空的,您应该在其中添加一些组件,例如 JButton、JLabel...。见How to Use Various Swing Components。

标签: java swing intellij-idea jframe jmenubar


【解决方案1】:

添加 label1 和 button1 - 您可以修复布局等。建议您也使用 Jpanel。

公共类环境{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame;
        JMenu jmenu;
        JMenuItem menuItem;
        JMenuBar menuBar;


        frame = new JFrame("Notepad");

            menuBar = new JMenuBar();
            menuBar.setVisible(true);
            jmenu = new JMenu("Test");
            menuItem = new JMenuItem("Open");

            jmenu.add(menuItem);

            JLabel label1 = new JLabel("My Name");
            JButton button1 = new JButton("Button");

            frame.add(label1);
            frame.add(button1);

            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new FlowLayout());
            frame.setSize(660, 350);

                // Set a main menu
                frame.setJMenuBar(menuBar);
                menuBar.add(jmenu);

        frame.setVisible(true);
    }
    }

【讨论】:

  • 谢谢。有用。这是一个奇怪的。在官方文档中,我没有找到有关所需图形元素的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多