如何为GridBagLayout中的所有组件设置间隙?

时间:2012-10-09 08:49:39

标签: java swing user-interface layout gridbaglayout

使用组布局时,您可以使用以下方法设置所有间隙:

setAutoCreateGaps(true);
setAutoCreateContainerGaps(true);

GridBagLayout是否有相同的功能?

2 个答案:

答案 0 :(得分:3)

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

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

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

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

  4. 例如:

    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
    

答案 1 :(得分:2)