gpt4 book ai didi

java - JMenu 不可见

转载 作者:行者123 更新时间:2023-12-01 23:51:22 24 4
gpt4 key购买 nike

我的 JFrame 中有一个 JMenu 和 JPanel

设置代码:

public Gui(String title) {
super(title);

createGUIComponents();
pack();

this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setResizable(true);
this.setMinimumSize(new Dimension(180, 100));
this.setSize(new Dimension(800, 600));

this.setVisible(true);
}

private void createGUIComponents() {
Container c = this.getContentPane();

JPanel panel = new SpecialJPanel();

JMenuBar menu = new JMenuBar();
fileMenu = new JMenu("File", false);
fileMenu.add("New");
fileMenu.add("Open");
fileMenu.add("Save");
fileMenu.add("Save As");

c.add(panel, "Center");
c.add(menu, "Center");
}

每当我单击 JMenuBar 上的"file"菜单按钮时,都不会显示任何内容。我认为它被不断更新的 JPanel 阻止了。有什么办法可以解决这个问题吗?

最佳答案

您没有将菜单添加到菜单栏,因此添加以下行:

menu.add(fileMenu);


另外,您应该使用 c.add(menu),而不是

setJMenuBar(menu);

关于java - JMenu 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16269932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com