将菜单栏添加到JFrame [英] Adding Menubar to JFrame

查看:678
本文介绍了将菜单栏添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下源代码,但我不明白为什么我的菜单栏/菜单不会显示在JFrame上,这对编程来说有些新意

I have the following source code and i just dont get why my menubar/menu wont show on the JFrame, im somewhat new to programming

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);

    }
}

推荐答案

我不确定100%为什么不显示JMenu的原因,但这可能是因为JMenu的内容不存在因此它们不会被渲染.

I am not 100 % sure why the JMenu's don't show up but it's probably because there are no items in the JMenu's and therefore they are not rendered.

所以这是您创建JMenuBarMenu而不是JMenuItems的问题所在.这就是创建JMenuBar:

So this is what was going wrong you created the JMenuBar the Menu's but not the JMenuItems. This is how you create a 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);

我希望这会有所帮助:)

I hope this helps :)

这篇关于将菜单栏添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆