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:
Code :
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?
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)
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:
Code :
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?
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:
Code :
getContentPane().setLayout(new FlowLayout());
add(firstComponent);
add(secondComponent);
Or, create a Box (or use Box Layout) - in pseudo-code:
Code :
Box box = Box.createVerticalBox();
box.add(firstComponent);
box.add(secondComponent);
myFrame.getContentPane().add(box);