gpt4 book ai didi

java - gridbaglayout垂直间隙

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:43 24 4
gpt4 key购买 nike

我正在制作一个带有一些按钮的面板。 this is what it looks like now.

但我希望按钮相互接触,换句话说,我希望垂直间隙消失。但不应调整按钮的大小。我该如何继续执行此操作?

我的代码:

this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();

constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = 2;
constraints.weightx = 1D;
constraints.weighty = 1D;
constraints.gridx = 0;
constraints.gridy = 0;
int row = 0;
for (MainAction actie : mainActions) {
constraints.gridy = row;
row++;
SelectedActionButton but = new SelectedActionButton(selectedActionModel, actie);
this.add(but, constraints);

}
constraints.gridwidth = 1;
constraints.weightx = 0.5D;
row *=2;
for (AbstractAction actie : objectActions) {
constraints.gridy = row/2;
constraints.gridx = row%2;
row++;
SelectedObjectButton but = new SelectedObjectButton(selectedObjectModel, actie);
but.setMargin(new Insets(0, 0, 0, 0));
this.add(but, constraints);
}

提前致谢

最佳答案

更改以下行

constraints.weighty = 1D;

constraints.weighty = 0D;

将解决您的问题。

更新:要将所有按钮向北对齐,您可以在最后一行添加一个额外的组件,占用所有可用空间:

constraints.weighty = 1D;
constraints.gridy = row;
this.add(Box.createGlue(), constraints);

关于java - gridbaglayout垂直间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5603858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com