【发布时间】:2023-03-15 04:49:01
【问题描述】:
我有一个 GridBagLayout JPanel,里面有一些组件,但是它们之间的空白太多了。
float wx = 1f, wy = 1f;
optionsPanel.add(optionsPanelLanguageSelectorInfo,
new GridBagConstraints(0, 0, 1, 1, wx, wy,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 100, 0, 10), 0, 0));
optionsPanel.add(optionsPanelLanguageSelector,
new GridBagConstraints(1, 0, 1, 1, wx, wy,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 10, 0, 100), 0, 0));
optionsPanel.add(optionsPanelAutoSaveToRecentSelectorInfo,
new GridBagConstraints(0, 1, 1, 1, wx, wy,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 100, 0, 10), 0, 0));
optionsPanel.add(optionsPanelAutoSaveToRecentSelector,
new GridBagConstraints(1, 1, 1, 1, wx, wy,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 10, 0, 100), 0, 0));
optionsPanel.add(optionsPanelAutoSecurityCheckSelectorInfo,
new GridBagConstraints(0, 2, 1, 1, wx, wy,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 100, 0, 10), 0, 0));
optionsPanel.add(optionsPanelAutoSecurityCheckSelector,
new GridBagConstraints(1, 2, 1, 1, wx, wy,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 10, 0, 100), 0, 0));
我试图改变 weightx 和 weighty 但没有效果。更改 ipadx 和 ipady 也无济于事。我是 GridBagLayout 的新手,所以我不知道如何解决这个问题。 请帮忙!
更改布局管理器也不适合我,因为我要在面板中添加更多组件。
解决方案: 是的,正如我所料,原因在于 weightx 和 weighty 值。在我的情况下,将 weighty 设置为 0 解决了这个问题。希望这会对某人有所帮助
【问题讨论】:
-
weightx 在这里很重要,因为您使用的是
GridBagConstraints.HORIZONTAL填充约束,但 weighty 没有效果,因为您没有使用GridBagConstraints.BOTH填充约束。如需更详细的答案,请考虑在您的问题中创建并发布有效的minimal reproducible example。这不是在此站点上获得答案的绝对要求,但通常可以帮助加快处理速度。 -
在我得到 weightx/weighty 后编辑评论
-
你试过零权重吗?你确定差距不在添加的组件中吗? (假设您不是指的是插图产生的间隙)
标签: java swing awt layout-manager gridbaglayout