Java 如何消除GridBagLayout中的间隙?

Java 如何消除GridBagLayout中的间隙?,java,swing,awt,layout-manager,gridbaglayout,Java,Swing,Awt,Layout Manager,Gridbaglayout,我有一个GridBagLayout JPanel,其中包含一些组件,但是它们之间有太多的空白 float wx = 1f, wy = 1f; optionsPanel.add(optionsPanelLanguageSelectorInfo, new GridBagConstraints(0, 0, 1, 1, wx, wy, GridBagConstraints.CENTER, GridBagConstraints

我有一个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的新手,所以我不知道如何解决这个问题请帮忙

更改布局管理器也不适合我,因为我将向面板添加更多组件

解决方案:
是的,正如我所料,原因在于权重x和权重值。在我的例子中,将权重设置为0解决了问题。希望这对某人有所帮助组件之间的空白可能是由以下原因造成的。因为这正是它们的用途。从javadoc:

Insets对象是容器边界的表示形式。它指定容器在其每个边上必须保留的空间。空格可以是边框、空格或标题

如果您将
插图中的所有条目更改为
0
,则空白处应消失:

optionsPanel.add(optionsPanelLanguageSelectorInfo,
        new GridBagConstraints(0, 0, 1, 1, wx, wy,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0),//<- important part
                0, 0));
选项面板.add(选项面板语言选择器或信息,
新的GridBagConstraints(0,0,1,1,wx,wy,
GridBagConstraints.CENTER、GridBagConstraints.HORIZONTAL、,

新插图(0,0,0,0)/WebX在这里很重要,因为您使用的是代码> GRIDBAG约束。水平填充约束,但是权重不起作用,因为您没有使用<代码> GRIDBAG约束。都是填充约束。为了更详细的答案,请考虑在您的问题中创建和发布一个有效的。这不是绝对的要求。在本网站上获得答案,但通常有助于加快进度。在我获得权重X/权重backwards时编辑的注释您尝试过零权重吗?您确定添加的组件中没有间隙吗?(假设您不是指插图产生的间隙)不,原因不在这里。在这种情况下,你应该编辑你的问题并添加一段代码,以便其他人可以测试它。否则我们只是猜测。