简体   繁体   中英

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

例

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);
}

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

将其添加到构造器中:setLayout(new FlowLayout(FlowLayout.CENTER));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM