【发布时间】:2017-12-23 17:22:04
【问题描述】:
我正在编写一个基本程序来练习和学习使用摆动 GUI,我已经构建了一个带有基本菜单的框架,将其添加到框架中,但是由于某种原因,当我运行该程序时它没有出现在框架。
public class GUITest {
private static int windowWidth = 500;
private static int windowHeight = 500;
private static JFrame frame;
private static JMenuBar menuBar;
public static void main(String[] args){
build();
}
private static void build(){
windowGen();
menuGen();
}
private static void windowGen(){
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setSize(windowWidth,windowHeight);
frame.setVisible(true);
}
private static void menuGen(){
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuFileExit = new JMenuItem("Exit");
menuFile.add(menuFileExit);
menuBar.add(menuFile);
frame.setJMenuBar(menuBar);
}
}
你们中有人知道为什么会这样吗?
【问题讨论】:
-
在显示框架之前添加它
-
对,
setVisible(true)必须是您调用的最后一件事,因为那是所有组件都布置好的时候。如果需要修改菜单栏,那就有点不一样了。 -
啊啊啊谢谢你=]
标签: java swing user-interface menu frame