简体   繁体   English

GridBagLayout错误地显示元素

[英]GridBagLayout displays elements incorrectly

I'm currently writing a test function, that dynamically adds elements into JPanel on button click. 我当前正在编写一个测试函数,该函数可以在单击按钮时将元素动态添加到JPanel I'm using GridBagLayout . 我正在使用GridBagLayout Here is the code of my function: 这是我的函数的代码:

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

In the first group of elements everything works fine. 在第一组元素中,一切正常。 They lay into the line from top-left corner(0,0). 它们从左上角(0,0)进入直线。

But in second group of elements their starting position becomes center of the panel(in Y-axis) and end of the Label4 (in X-axis). 但是在第二组元素中,它们的起始位置成为面板的中心(在Y轴上)和Label4的末端(在X轴上)。 But what i'm trying to achieve is to set their starting position to (0, 1). 但是我想要实现的是将其起始位置设置为(0,1)。 So this group is displayed directly below first one. 因此,该组直接显示在第一个组的下方。 Can anyone show me what i'm doing wrong? 谁能告诉我我在做什么错?

You need to set gridX and gridY values in your grid constraints or the layout manager has no idea where to put the components. 您需要在网格约束中设置gridXgridY值,否则布局管理器不知道将组件放置在何处。

And I think you are also confusing weightX and weightY with gridX and gridY. 而且我认为您还会将weightXweightY与gridX和gridY混淆。 The are two different things. 这是两件事。

For each component reset the constraints like this : 对于每个组件,重置约束如下:

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

If you don't know how long the list is, then you need to add some loops which will continuously increment the y. 如果您不知道列表有多长,则需要添加一些循环,这些循环将使y连续递增。

Alternatively you could add the two rows to two flowlaout panels, and then add this to a gridbaglaout panel like so : 或者,您可以将两行添加到两个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);

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

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