Probably still doing something stupid
Trying to make a GUI, and am adding a menu to the top using Swing. I've got the menu there, but I can't work out how to make it the size of the window.
Quote:
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menuBar.add(menu);
menuItem = new JMenuItem("A menu item");
menu.add(menuItem);
add(menuBar);
Quote:
add(panel);
setSize(500,500);
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Pizza");
setVisible(true);
The menu is displaying as the same size as a button I have, and inline with the button. I'd like it to have a width of 500 and be at the top, so it matches the width of the frame like a normal menu. I tried menuBar.setSize which didn't work and I can't find anything online :(
Re: Probably still doing something stupid
'normal' menus take as much room as needed, no more no less. Try 'setPreferredSize' instead of 'setSize' (this is just a quess though, the menu might resize itself, I don't know).
kind regards,
Jos
Re: Probably still doing something stupid
http://i43.tinypic.com/e67fap.png is how the menu is displaying :(
Re: Probably still doing something stupid
Anything wrong with that? It' a small menu with only one item in it, that's how those menus display themselves.
kind regards,
Jos
Re: Probably still doing something stupid
I'd like it to be the width of the frame pushed against the top
Re: Probably still doing something stupid
Try my guess from reply #2.
kind regards,
Jos
Re: Probably still doing something stupid
Re: Probably still doing something stupid
What layout manager is set for your frame? By default it is Border
layout. Have you chage it?
Re: Probably still doing something stupid
Don't add(...) a JMenuBar. Set (hint, hint) it.
I often wonder why people prefer to ask a Swing related question on a forum and get varied advice subject to how well they ask the question, whether they post relevant code etc. when it's so much faster to read the API and click the links to the various Swing tutorials.
db
Re: Probably still doing something stupid
Moved from New to Java
db