user2116860
user2116860

Reputation:

How do I put JPanel on another JPanel?

I am new to the java (and programming in general) and I am trying to make my very first program. I'm stuck on the same problem for about 5 hours now, so I've decided to ask for help.

Basically I'm trying to make a program (2d game) that has about 20 positions on the board. Each position is either blue (owned by player1), red(owned by player2) or black(not owned by anyone).

The way I'm going about this is in main I've put a method that calls setup game, and then a method that plays game. I am working on the setup game, basically all it does is it makes an object of class Background (extends JPanel, and overrides paintComponent()) and 20 objects of class Position(extends JPanel, and overrides paintComponent()).

So far I'm stuck on putting those Position objects on top of Background object.

When I do:

Background background= new Background();
frame.getContentPane().add(background);
Position position1= new Position;
frame.getContentPane().add(position1);
frame.setVisible(true);

it shows only a circle and no background as I was hoping, if I first add position and then background, I only have background and no circle.

Anyway I'm new to the java and I am still having trouble founding my way around, however I've tried to search for solutions, and I've found many different solutions to this problem (such as adding position to background first, and then adding background to frame, etc.) but I couldn't make any of them to work.

I am aware that the way I am adding them both to frame is (very likely) completely wrong, but I wrote it that way so you would (hopefully) be sure that what I've wrote actually does show you that my code for each of those classes draws something on the screen.

PS: I didn't copy my code here as most of variable and method names aren't in English so it's fairly hard to read, but if you still think its needed, I will add it. Also I'm sorry for my probably stupid question, but I'm kinda hitting a wall here and I've no idea what else to try.

Upvotes: 4

Views: 1649

Answers (5)

Halfwarr
Halfwarr

Reputation: 8123

It sounds like you want to use one JFrame and attach JPanels to it. This is how I personally would do it.

Declare your JFrame and JPanels

JFrame frame1 = new JFrame( "App Name");
JPanel panel1 = new JPanel(); 
JPanel panel2 = new JPanel(); 
JPanel panel3 = new JPanel(); 
JPanel panel4 = new JPanel(); 

Set the Background (I'm using colors but you get the idea)

panel1.setBackground(Color.orange);
panel2.setBackground(Color.orange);
panel3.setBackground(Color.orange);
panel4.setBackground(Color.orange);

Set your layout for the JFrame (I'm using BoxLayout not sure which would be best for you) You can find the best one for you and some sample code here. Also just set the default close operation.

frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame1.setLayout( new BoxLayout( frame1.getContentPane(), BoxLayout.Y_AXIS ) );

Then Just attach your JPanels

frame1.add( panel1);
frame1.add( panel2);
frame1.add( panel3);
frame1.add( panel4);
frame1.pack();
frame1.setVisible( true );

This will allow you to use the JPanels you created and then change the colors via other methods.

Upvotes: 0

camickr
camickr

Reputation: 324207

Background background= new Background();
frame.getContentPane().add(background);
Position position1= new Position;
frame.getContentPane().add(position1);

A JFrame uses a BorderLayout by default. Also by default when you add a component to a Container that uses a BorderLayout the comopnent is added to the CENTER. Only one comonent can be added to the CENTER so your Position comonent replaces the Background component.

You want to add the Position to the Background and then add the Background to the frame. Something like:

Background background= new Background();
Position position1= new Position;
background.add(position1);
frame.add(background);

Note: there is no need to uses getContentPane() when adding a component to the frame.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109823

Basically I'm trying to make a program (2d game) that has about 20 positions on the board. Each position is either blue (owned by player1), red(owned by player2) or black(not owned by anyone).

  1. Painting in Swing by default never returns PreferredSize, is required to override getPreferedSize()

  2. JPanel has implemented FlowLayout in API, this LayoutManager accepting only PreferredSize came from JComponents added to this container

  3. after a.m. changes to post an SSCCE, short, runnable, compilable

Upvotes: 2

Stephen Tetreault
Stephen Tetreault

Reputation: 787

Try using something like

jPanelExampleName.validate();
jPanelExampleName.repaint();

after adding your JPanels.

Upvotes: 0

roguequery
roguequery

Reputation: 974

The root panel should be a JFrame with a Container class underneath. When you call someRoot.window.container = yourJPanel, that loads the JPanel as the main component view of the JFrame. Note, a JFrame can only hold one JPanel but other JPanels can hold other JPanels. Just as you add the initial JPanel to the JFRam, a JPanel's own container can be another JPanel. Hope this helps.

Like this:

JPanel temp = new JPAnel();
frame.getContentPane().add(temp);
temp.getContentPane().add(new JPanel());

After these additions, there is a command that is illuding me but you call on JFrame to get it to refresh in real time. I think it is something like:

frame.validate();  //thanks @SMT

or something,

Upvotes: 0

Related Questions