2
有時候我的菜單上有兩個元素,有時候都是,但如果元素不可見,我需要調整窗口大小。如果我調整窗口的大小,就會出現元素。爲什麼?爲什麼我無法看到菜單欄的所有元素?
這是我的代碼:
//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
當出現錯誤:
後,我調整窗口的大小:
你可以在sendMenuBar之後調用senUI。 – StackFlowed 2014-08-29 12:54:14
謝謝@Aeshang。這是問題所在。 – 2014-08-29 12:56:07
如果它適合您,請將答案標記爲正確。 – StackFlowed 2014-08-29 15:42:56