Java Swing GridBagLayout can';t分配(删除)空空间

Java Swing GridBagLayout can';t分配(删除)空空间,java,swing,Java,Swing,尽管我读过一些关于weighty的文章,但鉴于下面的代码片段,我无法删除空格: JPanel panel = new JPanel(); scrollPane.setViewportView(panel); GridBagLayout gbl_panel = new GridBagLayout(); GridBagConstraints gc = new GridBagConstraints (); gbl_panel.columnWidths = new

尽管我读过一些关于weighty的文章,但鉴于下面的代码片段,我无法删除空格:

    JPanel panel = new JPanel();
    scrollPane.setViewportView(panel);
    GridBagLayout gbl_panel = new GridBagLayout();
    GridBagConstraints gc = new GridBagConstraints ();
    gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0};
    gbl_panel.rowHeights = new int[]{0, 0};
    gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
    panel.setLayout(gbl_panel);
    for(int i = 0;i<3;i++){
        gc.gridy = i;
        panel.add(new JButton("hi"),gc);
    }
JPanel面板=新的JPanel();
scrollPane.setViewportView(面板);
GridBagLayout gbl_面板=新建GridBagLayout();
GridBagConstraints gc=新的GridBagConstraints();
gbl_panel.columnWidths=新的int[]{0,0,0,0};
gbl_panel.rowHeights=newint[]{0,0};
gbl_panel.columnWeights=新的double[]{0.0,0.0,0.0,double.MIN_值};
gbl_panel.rowWeights=新的double[]{0.0,double.MIN_值};
面板。设置布局(gbl_面板);

对于(inti=0;i您需要
gridbagstraints
fill
字段

来自JavaDocs

 * This field is used when the component's display area is larger
 * than the component's requested size. It determines whether to
 * resize the component, and if so, how.
fill
定义组件是否应调整大小以及如何调整大小

weight
定义组件应使用的额外空间量

更新:


在所有按钮后添加任何JComponent,例如
JPanel
,并为
JPanel
weight=1设置
fill
。按钮的权重为0,因此所有额外的空间都添加到面板中。

但我不想调整按钮大小。我只想填补空白并逐行设置。(从上到下所有空格应位于底部,而不是按钮之间)顺便说一句,感谢您的关注。它会填充和删除空格,但仍会调整我的按钮大小以填充所有空格:(我不想调整大小。只想删除我的组件之间的所有间隙,并将这些间隙添加到面板的末尾。我上传了两张图片,无需张贴图片。请阅读这里的什么是SSCE并创建可运行示例您的
行权重
数组有两个元素。该数组中的第二个元素是非零权重,因此seco毫不奇怪布局中的第nd行被分配了额外空间。如何删除分配的额外空间?请为每行指定行权重。权重为零的任何行都不会被分配额外空间。