简体   繁体   English

如何在另一个JPanel的中心添加一个JPanel?

[英]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 我想像这样在其中心的另一个JPanel WorldView顶部添加一个JPanel地图

例

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 您可以使用GridBagLayout并修改GridBagConstraints#insetsEmptyBorder以与GridBagLayout或其他诸如BorderLayout布局结合使用在组件周围生成空白

See How to Use GridBagLayout and How to Use Borders for more details 有关更多详细信息,请参见如何使用GridBagLayout如何使用边框

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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