我正在编写一个测试函数,在按钮点击时动态地将元素添加到JPanel
。我正在使用GridBagLayout
。这是我的功能代码:
public void buildGui(){
ConnectionInfoPanel.removeAll();
//test first group of items
listOfLabels.add(new JLabel("Label 1"));
listOfLabels.add(new JLabel("Label 2"));
listOfLabels.add(new JLabel("Label 3"));
listOfLabels.add(new JLabel("Label 4"));
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.insets = new Insets(10,10,10,10);
c.anchor = GridBagConstraints.NORTHWEST;
ConnectionInfoPanel.add(listOfLabels.get(0), c);
ConnectionInfoPanel.add(new JLabel(), c);
ConnectionInfoPanel.add(listOfLabels.get(1), c);
ConnectionInfoPanel.add(new JLabel(), c);
ConnectionInfoPanel.add(listOfLabels.get(2), c);
ConnectionInfoPanel.add(new JLabel(), c);
ConnectionInfoPanel.add(listOfLabels.get(3), c);
ConnectionInfoPanel.add(new JLabel(), c);
//test second group
listOfLabels.add(new JLabel("Label 5"));
listOfLabels.add(new JLabel("Label 6"));
listOfLabels.add(new JLabel("Label 7"));
listOfLabels.add(new JLabel("Label 8"));
GridBagConstraints x = new GridBagConstraints();
x.weightx = 1;
x.weighty = 2;
x.insets = new Insets(10,10,10,10);
// x.anchor = GridBagConstraints.NORTHWEST;
ConnectionInfoPanel.add(listOfLabels.get(4), x);
ConnectionInfoPanel.add(new JLabel(), x);
ConnectionInfoPanel.add(listOfLabels.get(5), x);
ConnectionInfoPanel.add(new JLabel(), x);
ConnectionInfoPanel.add(listOfLabels.get(6), x);
ConnectionInfoPanel.add(new JLabel(), x);
ConnectionInfoPanel.add(listOfLabels.get(7), x);
ConnectionInfoPanel.add(new JLabel(), x);
ConnectionInfoPanel.updateUI();
}
在第一组元素中一切正常。它们从左上角(0,0)开始排列。
但是在第二组元素中,它们的起始位置成为面板的中心(在Y轴上)和Label4的末端(在X轴上)。但我想要实现的是将他们的起始位置设置为(0,1)。所以这个组直接显示在第一个组之下。谁能告诉我我做错了什么?
答案 0 :(得分:1)
您需要在网格约束中设置 gridX 和 gridY 值,否则布局管理器不知道放置组件的位置。
而且我认为你还使用gridX和gridY来混淆 weightX 和 weightY 。这是两件不同的事情。
对于每个组件,重置如下约束:
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
ConnectionInfoPanel.add(listOfLabels.get(0), c);
c.gridx++;
ConnectionInfoPanel.add(listOfLabels.get(1), c);
c.gridx++;
ConnectionInfoPanel.add(listOfLabels.get(2), c);
c.gridx++;
ConnectionInfoPanel.add(listOfLabels.get(3), c);
c.gridx = 0;
c.gridy = 1;
ConnectionInfoPanel.add(listOfLabels.get(4), c);
c.gridx++;
ConnectionInfoPanel.add(listOfLabels.get(5), c);
c.gridx++;
ConnectionInfoPanel.add(listOfLabels.get(6), c);
c.gridx++;
ConnectionInfoPanel.add(listOfLabels.get(7), c);
如果你不知道列表有多长,那么你需要添加一些循环来连续递增y。
或者你可以将两行添加到两个flowlaout面板,然后将其添加到gridbaglaout面板,如下所示:
JPanel panel1 = new JPanel();
panel1.add(listOfLabels.get(0));
panel1.add(listOfLabels.get(1));
panel1.add(listOfLabels.get(2));
panel1.add(listOfLabels.get(3));
ConnectionInfoPanel.add(panel1, c);
JPanel panel2 = new JPanel();
panel2.add(new JLabel("Label 5"));
panel2.add(new JLabel("Label 6"));
panel2.add(new JLabel("Label 7"));
panel2.add(new JLabel("Label 8"));
GridBagConstraints x = new GridBagConstraints();
x.weightx = 1;
x.weighty = 2;
x.insets = new Insets(10, 10, 10, 10);
// x.anchor = GridBagConstraints.NORTHWEST;
x.gridy = 0;
ConnectionInfoPanel.add(panel1, x);
x.gridy = 1;
ConnectionInfoPanel.add(panel2, x);