0

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); 
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tian Zhang
  • 13
  • 1
  • 3

2 Answers2

2

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.

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
1

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 );
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54