Java 如何使用 Swing 修复元素的显示
How to fix the display of elements using Swing in Java
我正在 Java 中使用 Swing 开发一个应用程序,但是,我在显示中遇到了一个奇怪的问题,我的代码工作得很好,但输出很奇怪。当我运行程序。 JFrame 看起来是空的或缺少某些东西(JMenuBar、JMenuItem 等是不可见的),然后我最大化屏幕,所有其他东西都变得可见,然后我最小化屏幕,它看起来是可见的。我很确定代码工作正常,这只是一个显示问题。谁能帮忙让第一个显示器看起来不错?
这是代码
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
把下面的代码放在最后。它会很好地工作。
方法setVisible
是一个动作,就像JDK之前的show()
1.5.
frame.setVisible(true);
我正在 Java 中使用 Swing 开发一个应用程序,但是,我在显示中遇到了一个奇怪的问题,我的代码工作得很好,但输出很奇怪。当我运行程序。 JFrame 看起来是空的或缺少某些东西(JMenuBar、JMenuItem 等是不可见的),然后我最大化屏幕,所有其他东西都变得可见,然后我最小化屏幕,它看起来是可见的。我很确定代码工作正常,这只是一个显示问题。谁能帮忙让第一个显示器看起来不错?
这是代码
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
把下面的代码放在最后。它会很好地工作。
方法setVisible
是一个动作,就像JDK之前的show()
1.5.
frame.setVisible(true);