【发布时间】:2015-02-18 05:39:31
【问题描述】:
好的,我有一个带有 JMenuBar 的 GUI,当我加载它时,有时它不会显示,但如果我将其最小化并重新点击它,JMenuBar 就会显示。我的问题在哪里?我该如何解决?哦,如果我重新调整大小,JMenuBar 就会出现。
这是我的代码
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("TwitchBot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(700, 500));
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.pack();
frame.setVisible(true);
KeyGetter.LoadKeys();
try {
Config.loadConfig();
} catch (Exception e) {
e.printStackTrace();
}
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
mb.add(file);
JMenu edit = new JMenu("Edit");
mb.add(edit);
JMenuItem options = new JMenuItem("Options");
options.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Config.openConfig(frame);
}
});
frame.setJMenuBar(mb);
edit.add(options);
}
}
【问题讨论】:
-
你应该
pack()并在你添加所有元素后显示JFrame。
标签: java user-interface jmenubar