【发布时间】:2016-01-25 14:47:00
【问题描述】:
当我运行程序时,我看到一个空白窗口。 我该如何解决这个问题?谢谢。我做错了什么? 这是我的代码:
public class Environment{
private JFrame frame;
private JMenu jmenu;
private JMenuItem menuItem;
private JMenuBar menuBar;
Environment(){
frame = new JFrame("Notepad");
menuBar = new JMenuBar();
//menuBar.setVisible(true);
jmenu = new JMenu("Test");
menuItem = new JMenuItem("Open");
jmenu.add(menuItem);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(660, 350);
// Set a main menu
frame.setJMenuBar(menuBar);
menuBar.add(jmenu);
frame.setVisible(true);
}
}
【问题讨论】:
-
菜单显示正确,你的框架是空的,因为你没有在里面添加任何组件。
-
@NarutoBijuMode 你好。像按钮、标签等?
-
是的,框架是空的,您应该在其中添加一些组件,例如 JButton、JLabel...。见How to Use Various Swing Components。
标签: java swing intellij-idea jframe jmenubar