gridbaglayout解释我的代码

时间:2014-03-27 06:37:28

标签: java swing gridbaglayout

this.getContentPanel().add(sortOrderScrollPane, new GridBagConstraints(0, 0, 5, 5, 0.0, 0.0,
    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(5, 5, 10,10), 0, 0));  
this.getContentPanel().add(toFirstSortButton, new GridBagConstraints(5, 4, 1, 1, 0.0, 0.0,
    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 5, 5, 10), 0, 0));

toFirstSortButton占据了整个列。任何人都可以解释原因吗?

1 个答案:

答案 0 :(得分:1)

GridBagConstraints.BOTH更改为GridBagConstraints.NONE

此外,我建议使用以下示例中的GridBagLayout。它更容易阅读并防止错误,因为您不再需要搜索正确的参数。

this.getContentPanel().setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 1;
gbc.insets = new Insets(0, 0, 0, 0);
this.getContentPanel().add(sortOrderScrollPane, gbc);
//etc..