【问题标题】:Adding Menubar to JFrame将菜单栏添加到 JFrame
【发布时间】:2015-03-15 14:50:44
【问题描述】:

我有以下源代码,但我只是不明白为什么我的菜单栏/菜单不会显示在 JFrame 上,我对编程有点陌生

public class drawingApp {
    public static void main(String[] args) {

        JFrame frame = new JFrame("DrawingApp");
        frame.setSize(600,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

        JMenuBar mb = new JMenuBar();
        JMenu menu1 = new JMenu("Colour");
        mb.add(menu1);
        JMenu menu2 = new JMenu("Size");
        mb.add(menu2);

        frame.setJMenuBar(mb);

    }
}

【问题讨论】:

    标签: java jframe menubar jmenu


    【解决方案1】:

    我不能 100% 确定为什么 JMenu 没有出现,但这可能是因为 JMenu 中没有项目,因此它们没有被渲染。

    所以这就是你创建JMenuBar Menu 而不是JMenuItems 的问题所在。这就是您创建JMenuBar 的方式:

    JFrame myframe = new JFrame();
    JMenuBar menubar = new JMenuBar();
    JMenu menu = new JMenu("size");
    JMenuItem size = new JMenuItem("size");
    menu.add(size);
    menubar.add(menu);
    myframe.setJMenuBar(menubar);
    

    我希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      在框架设置为可见后添加菜单栏。因此,首先渲染框架,然后添加菜单栏。试试:

      frame.setJMenubar(mb);
      frame.validate();
      frame.repaint();
      

      这应该可以解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        • 2011-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多