有时候我的菜单上有两个元素,有时只有元素不可见,我需要调整窗口大小。如果我调整窗口大小,则会显示元素。为什么?
这是我的代码:
//Class TextEditor start
public class TextEditor extends JFrame{
private JMenuBar menuBar;
private JMenu file,edit,format,view,help;
private JMenuItem newFile;
private JMenuItem exit;
//Main method start
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e){
}
new TextEditor();
}
//Main class end
//Class constructor start
private TextEditor(){
super("Untitled");
sendUI(this);
sendMenuBar();
}
//Class constructor end
//Menu bar start//
public void sendMenuBar(){
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//File menu and Items
file = new JMenu(" File ");
newFile = new JMenuItem("New");
exit = new JMenuItem("Exit");
menuBar.add(file);
file.add(newFile);
file.add(exit);
//Edit menu and items
edit = new JMenu(" Edit ");
menuBar.add(edit);
//Format menu and items
format = new JMenu(" Format ");
menuBar.add(format);
//View menu and items
view = new JMenu(" View ");
menuBar.add(view);
//Help menu and items
help = new JMenu(" Help ");
menuBar.add(help);
}
private void sendUI(TextEditor texteditor) {
texteditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
texteditor.setSize(700,400);
texteditor.setLocationRelativeTo(null);
texteditor.setVisible(true);
}
}
//Class TextEditor end
出现错误时:
我调整窗口大小后:
答案 0 :(得分:0)
您需要在sendMenuBar之后调用senUI。