1

我有一个主框架和 3 个面板。我想将这 3 个面板添加到主框架中。但是,其中只有 2 个被添加。第三个不是。我不知道为什么。有人可以帮忙吗?

        setLayout(new GridBagLayout());
        GridBagConstraints gbc=new GridBagConstraints();
        gbc.gridwidth=GridBagConstraints.REMAINDER;
        gbc.gridheight=GridBagConstraints.RELATIVE;
        gbc.anchor=GridBagConstraints.NORTHWEST;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.weightx=gbc.weighty=1;      
        add(topPanel1, gbc);
        add(bottomPanel1, gbc);
        gbc.gridheight=GridBagConstraints.REMAINDER;
        add(buttonsPanel, gbc);

上面的代码是一个框架的构造函数。

4

2 回答 2

1

您应该根据所需的布局设置gridxor值。gridy例如,如果您想垂直布局组件,请执行以下操作:

gbc.gridx=0;
gbc.gridy=0;
add(topPanel1, gbc);
gbc.gridy++;
add(bottomPanel1, gbc);
gbc.gridy++;
add(buttonsPanel, gbc);

如果您不设置gridx/y值,则行为是未指定的(有时它可能会起作用)。

于 2011-11-08T09:43:09.760 回答
1

gbc.gridheight=GridBagConstraints.RELATIVE;如果要一个接一个地添加三个面板,则必须删除该语句。

于 2011-11-08T09:56:53.367 回答