문제

I seem to have done everything correct, but it just cannot showing. Can anyone tell me why my menubar not showing? Can anyone help me????

public void go() {
    frame = new JFrame("Notepad");
    //Font defaultFont = new Font("Candara", 10, 0);
    textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane tScroller = new JScrollPane(textArea);
    tScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    JMenuBar menu = new JMenuBar();
    JMenu file = new JMenu("File");

    JMenuItem newNote = new JMenuItem("New");
    JMenuItem openNote = new JMenuItem("Open");
    JMenuItem saveNote = new JMenuItem("Save");
    JMenuItem saveAsNote = new JMenuItem("Save as...");
    file.add(newNote);
    file.add(openNote);
    file.add(saveNote);
    file.add(saveAsNote);

    frame.setJMenuBar(menu);
    frame.getContentPane().add(BorderLayout.CENTER, tScroller); 

    frame.pack();
    frame.setSize(800,700);
    frame.setVisible(true); 
}
도움이 되었습니까?

해결책

Right now, you are showing an empty menu bar. For a menu bar to show up correctly, you must first add some menu items into it. For example,

menu.add(file);

will instruct the menu bar to consider the "File" menu item, which should be visible now.

다른 팁

It is because you are not adding anything to the JMenuBar.

You can do it as follows,

menu.add(newNote);
menu.add(openNote);
menu.add(saveNote);
menu.add(saveAsNote);

//Or infact as suggested by the other answer,adding the menu file to the JMenuBar

menu.add(file );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top