user1576579
user1576579

Reputation: 13

How to add a JPanel in the center of another JPanel?

I Want to add a JPanel map on top of another JPanel WorldView in it's center just like this

Example

Here is my Code:

public class WorldView extends JPanel implements ActionListener{

private JPanel map;

public WorldView() throws IOException{
    this.setSize(1024,768);
    this.setBackground(Color.BLACK);
    map = new JPanel();
    this.add(map);
}

Upvotes: 0

Views: 238

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

You could use a GridBagLayout and modify the GridBagConstraints#insets or a EmptyBorder to generate the whitespace around the component in combination with GridBagLayout or some other layout like BorderLayout for example

See How to Use GridBagLayout and How to Use Borders for more details

Upvotes: 1

Attila Horváth
Attila Horváth

Reputation: 14

Add this in the constrctor : setLayout(new FlowLayout(FlowLayout.CENTER));

Upvotes: 0

Related Questions