Trouble with Java menubar in java frame.
Hey, I've got some trouble with my code. It should be two tabs in the frame, with the titles "File" and "Open". But I can only see "File". I rewrote the code and launched it, and I could see both, then I changed size of the frame, and they were gone again. Sometimes I can see neither one, it's really weird. Here's the code, take a look at it and see if you can see any mistakes. And I'm using Dr Java and Eclipse, neither of them works.
import javax.swing.*;
public class Meny{
public static void main(String[] cmdLn){
JFrame f = new JFrame("Meny");
f.setVisible(true);
f.setSize(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu file = new JMenu("File");
mb.add(file);
JMenuItem exit = new JMenuItem();
file.add(exit);
JMenu open = new JMenu("Open");
mb.add(open);
}
}
I can't see what I've done wrong. Help would be appreciated.
Edit
All of a sudden I can see the two tabs, but I can't see the "exit" option in the tab "File". It's really weird.
Re: Trouble with Java menubar in java frame.
Hi
You should provide label for exit menu item. i.e JMenuItem exit = new JMenuItem("Exit");
Re: Trouble with Java menubar in java frame.
I'm not sure what you mean, JLabel?
Anyway, it's working better now, but I can't understand why it's so messy.
Do you think it has anything to do with the size? At first, when I start the program, the tabs aren't there, but when i grab the edge and reducing/increasing the frame's size, they show up. Strange.
Re: Trouble with Java menubar in java frame.
Try waiting to call setVisible() until after everything is ready to be seen. The code adds to the GUI after it is called.
Please edit your post and wrap your code with code tags:
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Trouble with Java menubar in java frame.
Thank you very much, I will remember that.