使用GridBagLayout时组件重叠

时间:2013-04-03 08:16:37

标签: java swing jlabel gridbaglayout

在定位我的JLabels时,我发现了一个问题,直到现在我无法解决。 JLabels重叠。

这是关于gridbaglayout的代码片段:

   c = new GridBagConstraints();

   c.gridx = 0;
   c.gridy = 0;
   c.gridwidth = 3;
   c.fill = GridBagConstraints.HORIZONTAL;
   c.ipady = 20;
   c.weightx = 3;
   c.weighty = 1;
   c.insets = new Insets(0,5,5,5);

   all.add(header, c);

   c.gridx = 0;
   c.gridy = 1;
   c.weightx = 2;
   c.weighty = 4;
   c.insets = new Insets(5,5,5,5);

   all.add(sts, c); //this label overlapped

   c.gridx = 1;
   c.gridy = 1;
   c.weightx = 1;
   c.weighty = 5;
   c.insets = new Insets(5,5,5,5);

   all.add(cl, c); //this label overlapped

提前致谢

1 个答案:

答案 0 :(得分:3)

网格宽度为3意味着组件将在x网格中占据3个位置,请阅读网格包布局教程 http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

你有第一个组件在x0这将扩展3列到x2因此下一个可用的网格位置是x3