[英]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#insets
或EmptyBorder
以与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.