【问题标题】:How to set gaps for ALL Components in the GridBagLayout?如何为 GridBagLayout 中的所有组件设置间隙?
【发布时间】:2012-09-29 13:23:22
【问题描述】:

当您使用组布局时,您可以通过以下方式设置所有间隙:

setAutoCreateGaps(true);
setAutoCreateContainerGaps(true);

GridBagLayout有同样的功能吗?

【问题讨论】:

  • 你可能想看看 GridBagConstraints 的 insetsproperty

标签: java swing user-interface layout gridbaglayout


【解决方案1】:

在 GridBagLayout 中,使用 GridBagConstraints 您可以使用以下属性设置间隙;

  1. GridBagConstraints.ipadx,GridBagConstraints.ipady: 指定布局中组件的内部填充。

  2. GridBagConstraints.insets: 指定组件的外部填充。

  3. GridBagConstraints.weightx,GridBagConstraints.weighty: 用于确定如何分配空间。

例如:

pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;      //make this component tall
c.ipadx = 10;      //make this component wide
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //first column
c.gridy = 2;       //third row
c.gridwidth = 2;   //2 columns wide
c.weightx = 0.5;   //increase horizontal space
c.weighty = 1.0;   //increase vertical space

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2013-08-01
      • 2011-08-02
      • 1970-01-01
      • 2015-08-21
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多