简体   繁体   English

GridBagLayout上下调整间隙

[英]GridBagLayout top and bottom resizing gaps

I have following code: 我有以下代码:

public Frame() {
    super();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.weightx = 1;
    c.weighty = 1;
    for (int i = 0; i < 10; i++) {
        c.gridy = i;
        panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
        add(panel, c);
    }
}

Resulting in following frame: 结果如下:
以下

Due to resizing (intended) you can see there is some space above and below the top and bottom element(marked by arrows). 由于调整大小(预期),您可以看到顶部和底部元素上方和下方有一些空间(由箭头标记)。

Any idea how to get rid of both empty spaces or at the very least the top one? 任何想法如何摆脱两个空白空间,或者至少摆脱顶层空间?
Thanks 谢谢

ps: i'm aware this question is similar to a previous question of mine however it is not the same! ps:我知道这个问题与我以前的问题类似,但是不一样!

The problem has to do with how GridBagLayout manages extra vertical space, through GridBagConstraints.weighty property (see API). 问题与GridBagLayout如何通过GridBagConstraints.weighty属性管理额外的垂直空间有关(请参阅API)。 I'll just quote this line: 我只引用这行:

If all the weights are zero, all the extra space appears between the grids of the cell and the top and bottom edges . 如果所有权重均为零,则所有多余的空间都会出现在单元格的网格与顶部和底部边缘之间

The same happens when all weights are 1 (your case). 当所有权重均为1(您的情况)时,也会发生相同的情况。 In fact this will happen unless the first or last row (or both of them) be explicitly set to fill the whole extra space because of the way on how the layout manager calculates the available space. 实际上,除非布局管理器如何计算可用空间的方式明确地设置了第一行或最后一行(或两行)以填充整个额外空间,否则这将发生。 Presumably there's always extra space unassigned due to rest of division between available space and rows number and this space is distributed at the top (and/or) bottom edges. 由于可用空间和行数之间的剩余划分,可能总是存在未分配的额外空间,并且该空间分布在顶部(和/或)底部边缘。

If you replace your loop by next lines, then the whole extra space will be distributed in top and bottom rows without any gap, (but middle rows won't take any extra space): 如果用下一行替换循环,则整个额外空间将无间隙地分布在顶部和底部行中(但中间行将不占用任何额外空间):

    //c.weighty = 1; comment this line

    for (int i = 0; i < 10; i++) {
        c.gridy = i;
        c.weighty = (i == 0 || i == 9) ? 1 : 0; // add this line here
        panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
        frame.getContentPane().add(panel, c);
    }

You can play with this added line and you'll see what I mean. 您可以使用此添加的代码行,您会明白我的意思。

Note : if you want a well distributed grid then GridLayout might be a better choice. 注意 :如果您想要一个分布良好的网格,那么GridLayout可能是一个更好的选择。

Off-topic 无关

  • When you add components to a container which has already been displayed you need to call revalidate() method. 将组件添加到已经显示的容器中时,需要调用revalidate()方法。
  • You should always call pack() method before make your top-level container visible. 显示顶级容器之前,应始终调用pack()方法。

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

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