GridBagLayout附加组件

时间:2013-12-05 12:41:24

标签: java jpanel layout-manager gridbaglayout

看看那段代码:

    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTHEAST;
    c.weightx = 1;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 0;
    this.add(buttonMinus,c);

    c.anchor = GridBagConstraints.NORTH;
    c.gridx = 1;
    c.gridy = 0;
    this.add(labelTime,c);

    c.anchor = GridBagConstraints.NORTHWEST;
    c.gridx = 2;
    c.gridy = 0;
    this.add(buttonPlus,c);

    c.anchor = GridBagConstraints.SOUTHWEST;
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 1;
    this.add(labelCoor,c);

现在看起来如何: now

我想成为: need

ofc我想不要调整按钮或标签的大小。让他们保持默认大小。

我知道我可以通过在BorderLayout面板的北部添加一个新的FlowLayout面板,但我想用一个GridBagLayout

进行操作

1 个答案:

答案 0 :(得分:1)

ofc i want to not resize my buttons or labels. let them stay their default sizes.

然后您应该设置weightx = 0, weighty = 0,以便它们各自的行和列不会获得额外的空间,您当前将其设置为1GridBagLayout通常也会尊重首选尺寸,但我们应该指定首选尺寸和最小尺寸,如a use case of this question中所述。另请查看官方消息来源specifying constraint section了解更多详情。