Results 1 to 10 of 10
  1. #1
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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.

    menuBar = new JMenuBar();
    menu = new JMenu("A Menu");
    menuBar.add(menu);
    menuItem = new JMenuItem("A menu item");
    menu.add(menuItem);
    add(menuBar);
    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 :(

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Probably still doing something stupid

    http://i43.tinypic.com/e67fap.png is how the menu is displaying :(

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Probably still doing something stupid

    I'd like it to be the width of the frame pushed against the top

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Probably still doing something stupid

    Try my guess from reply #2.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Vicness is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Probably still doing something stupid

    Tried, doesn't work :(

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Probably still doing something stupid

    What layout manager is set for your frame? By default it is Border
    layout. Have you chage it?
    Last edited by milovan; 04-14-2012 at 11:36 PM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Probably still doing something stupid

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Probably doing something stupid
    By Vicness in forum New To Java
    Replies: 4
    Last Post: 04-14-2012, 07:10 PM
  2. Stupid Array are Stupid.
    By Army in forum New To Java
    Replies: 10
    Last Post: 04-13-2012, 06:51 PM
  3. Stupid mistake, maybe?
    By knox in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 08:39 AM
  4. Replies: 0
    Last Post: 02-15-2011, 11:16 PM
  5. a really stupid question
    By SwinGirl in forum NetBeans
    Replies: 10
    Last Post: 06-25-2008, 09:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •