【问题标题】:GridBagLayout adhering componentsGridBagLayout 附着组件
【发布时间】:2013-12-05 12:41:24
【问题描述】:

看看那个代码:

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

现在的样子:

我想成为什么样的人:

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

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

【问题讨论】:

    标签: java jpanel layout-manager gridbaglayout


    【解决方案1】:
    ofc i want to not resize my buttons or labels. let them stay their default sizes.
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 2012-02-10
      • 2018-01-13
      • 1970-01-01
      相关资源
      最近更新 更多