Tian Zhang
Tian Zhang

Reputation: 13

My JMenuBar not showing

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); 
}

Upvotes: 0

Views: 345

Answers (2)

Frederik.L
Frederik.L

Reputation: 5620

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.

Upvotes: 2

Shashank Kadne
Shashank Kadne

Reputation: 8101

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 );

Upvotes: 1

Related Questions