gpt4 book ai didi

java - JMenu 弹出窗口有时有效

转载 作者:行者123 更新时间:2023-12-01 22:33:31 26 4
gpt4 key购买 nike

我在使用 JMenu 弹出窗口时遇到问题。它只是有时有效。有时我根本看不到 java 弹出窗口。有时我的文件和编辑选项完全丢失。这就是我的代码的样子。

import javax.swing.*;

public class menu {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);

JMenuBar mb = new JMenuBar();

JMenu file = new JMenu ("File");
mb.add(file);
JMenu edit = new JMenu("Edit");
mb.add(edit);

f.setJMenuBar(mb);
}

}

最佳答案

仅在初始化核心 UI 后才在 JFrame 上调用 setVisible...

JFrame f = new JFrame();
// Don't call this here...
//f.setVisible(true);
f.setSize(400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);

JMenuBar mb = new JMenuBar();

JMenu file = new JMenu ("File");
mb.add(file);
JMenu edit = new JMenu("Edit");
mb.add(edit);

f.setJMenuBar(mb);
// Call it here
f.setVisible(true);

此外,请确保您仅在事件调度线程的上下文中创建/更新 UI,请参阅 Initial Threads了解更多详情

关于java - JMenu 弹出窗口有时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219509/

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