Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trouble adding components to a JFrame

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    GB
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble adding components to a JFrame

    Hi everyone. I'm trying to make a basic game where the mouse moves a ball around a JPanel and dodges other balls. The game is going fine, but I can't get anything to appear in my JFrame.

    Here's the goal for now: I'd like to have the JPanel holding the game in the upper left. Somewhere on top of it should be a JLabel with the text "Click to start".

    This seemed like a simple task. But when I tried just adding one component after the other, the first one always disappeared. Right now I'm trying to add both to a JLayeredPane, but I feel like that's overkill and I can't get it to show up at all.

    Here's my code so far:

    JLayeredPane pane=new JLayeredPane();
     
    //Add CollisionsPanel
    int numballs=10;
    CollisionsPanel collpanel = new CollisionsPanel(xBound, yBound, numBalls);
    pane.add(collpanel, 0);
     
    //Add starting text
    JLabel clickToStart=new JLabel("Click to start");
    clickToStart.setHorizontalAlignment(SwingConstants.CENTER);
    clickToStart.setVerticalAlignment(SwingConstants.CENTER);
    pane.add(clickToStart, 1);
     
    //Add layered pane to frame
    setLayeredPane(pane);

    Any ideas?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trouble adding components to a JFrame

    If you have not yet, I highly recommend reading the following:
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Choosing the appropriate LayoutManager - and nesting them if you have to - will give you the flexibility to organize your components however you wish. And note that by default a content pane uses a BorderLayout (which is the reason you are seeing the behavior you see - although I can't be for sure based solely upon the code you posted)

  3. #3
    Junior Member
    Join Date
    May 2012
    Location
    GB
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble adding components to a JFrame

    Thanks! I have read that, but none of the layouts look like they would help me here. What I did notice when you posted that was a title at the bottom of the tutorials -- "Doing without a layout manager" -- which I thought might be helpful.

    I tried doing the following:

    setLayout(null);
     
    int limXBound = xBound-Constants.SHRINK_X;
    int limYBound = yBound-Constants.SHRINK_Y;
     
    //Add CollisionsPanel
    collpanel = new CollisionsPanel(limXBound, limYBound, numBalls);
    collpanel.setBounds(0, 0, limXBound, limYBound);
    collpanel.repaint();
    add(collpanel);
     
    //Add starting text
    JLabel start=new JLabel("Click to start");
    Dimension dim = start.getPreferredSize();
    start.setBounds(limXBound/2-40, limYBound/2-30, (int)dim.getWidth(), (int)dim.getHeight());
    add(start);

    So far this is working better because I can set the positions of both objects where I want them. The problem is that I still can't place one on top of the other. Right now the JPanel shows up on top even if I add the JLabel after it.

    I'm hoping to program 3 other games like this, so figuring this out would be a huge step forward. What will fix this problem?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trouble adding components to a JFrame

    Layout Manager may seem daunting, but I would NOT recommend a null layout (I'm speaking from experience - it is a nightmare measuring components, placement, and then seeing those trashed when faced with multiple systems, fonts, window resizing, etc...). What about using Layouts didn't seem to help you? I'd recommend playing with them a bit and you should see they hold a LOT of power in creating a user interface. Again, you didn't post enough code to work with (posting an SSCCE helps), but try setting the content pane layout to flow layout. eg:

    getContentPane().setLayout(new FlowLayout()); 
    add(firstComponent);
    add(secondComponent);

    Or, create a Box (or use Box Layout) - in pseudo-code:
    Box box = Box.createVerticalBox();
    box.add(firstComponent);
    box.add(secondComponent);
    myFrame.getContentPane().add(box);

Similar Threads

  1. Trouble adding exceptions to fixed queue class
    By Farmer in forum Exceptions
    Replies: 5
    Last Post: December 19th, 2011, 07:23 AM
  2. Adding components to my GUI over a background image
    By derekxec in forum AWT / Java Swing
    Replies: 12
    Last Post: August 23rd, 2011, 03:01 PM
  3. [SOLVED] Add more components from JFrame Form
    By zecute in forum AWT / Java Swing
    Replies: 3
    Last Post: May 11th, 2011, 07:30 AM
  4. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  5. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 01:25 PM