How to put JPanel on another JPanel?

I am new to java (and generally programming), and I try to make my first program. I was stuck in the same problem for about 5 hours, so I decided to ask for help.

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

As I do this, basically I put in a method that calls the gameโ€™s settings, and then a method that plays the game. I am working on an installation game, basically all it does is make an object of the Background class (extends JPanel and overrides paintComponent() ) and 20 objects of the Position class (extends JPanel and overrides paintComponent() ).

So far, I'm stuck with placing these Position objects on top of the Background object.

When I do this:

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

it shows only the circle and background, as I had hoped, if I add the position first and then the background, I only have the background and the circle.

In any case, I am new to java and still have problems finding solutions, but I tried to find solutions and I found many different solutions to this problem (for example, adding a position to the background first and then adding the background to the frame and etc.), but I could not get them to work.

I know that the way that I add both of them to a frame is (very likely) completely wrong, but I wrote it in such a way that you (hopefully) are sure that what I wrote really shows you that my the code for each of these classes draws something on the screen.

PS: I did not copy my code here, since most of the variable names and methods are not written in English, so it is quite difficult to read, but if you still think it is necessary, I will add This. In addition, I regret my probably stupid question, but Iโ€™m kind of on the wall here, and I donโ€™t know what else to try.

+4
source share
5 answers

Basically I am 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).

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

JFrame uses BorderLayout by default. Also by default, when you add a component to a container that uses BorderLayout, comopnent is added to CENTER. Only one component can be added to CENTER so that your Position component replaces the background component.

You want to add a position to the wallpaper, and then add the background to the frame. Sort of:

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

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

+1
source

The root panel should be a JFrame with the Container class below it. When you call someRoot.window.container = yourJPanel, it loads the JPanel as the main component of the JFrame. Note that a JFrame can hold only one JPanel, but other JPanels can hold other JPanels. Just as you add an initial JPanel to JFRam, your own JPanel container may be a different 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 team that tricks me, but you call JFrame to update it in real time. I think this is something like:

 frame.validate(); //thanks @SMT 

or something,

0
source

Try using something like

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

after adding your JPanels.

0
source

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

Announce 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 use 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 JFrame (I use BoxLayout, not sure which will be better 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 using other methods.

0
source

All Articles