调整大小时避免单元格之间出现空白 space

Avoid blank space between cells when resize

我找不到适用于我的问题的任何内容,我测试的解决方案均无效。

所以,我正在为一门课程开发 UI,当我调整 window 的大小时,在单元格之间创建了一个巨大的空白 space,而不是结尾正如人们所期望的那样(也是我想要的)

我对面板进行了颜色编码:

因此,我在勃艮第面板上使用网格包,因为我希望字段和标签按原样对齐,这是我发现的最简单的方法。现在,正如您在红色圆圈中看到的,当 window 调整大小时会创建一个巨大的 space,为什么?谁在这样做?什么面板/配置是罪魁祸首?

这是我认为涉及的部分代码,如果您需要其他内容,请告诉我

 //----Display Panel (green box)
    public void createDisplayPanel(){
        JPanel dPanel = new JPanel();
        dPanel.setBorder(margins);
        add(dPanel, BorderLayout.EAST);
        dPanel.setLayout(new BoxLayout(dPanel, BoxLayout.Y_AXIS));

        dPanel.add(createDisplayTitlePanel()); //Purple box
        dPanel.add(createDisplayFieldsPanel());//Burgundy box
        dPanel.add(createDisplayControlsPanel());//Orange box
    }
    private JPanel createDisplayFieldsPanel(){
        JPanel dFieldsPanel = new JPanel();     
        GridBagLayout gbl_dFieldsPanel = new GridBagLayout();
        gbl_dFieldsPanel.columnWidths = new int[]{0};
        gbl_dFieldsPanel.rowHeights = new int[] {0};
        gbl_dFieldsPanel.columnWeights = new double[]{Double.MIN_VALUE};
        gbl_dFieldsPanel.rowWeights = new double[]{Double.MIN_VALUE};
        dFieldsPanel.setLayout(gbl_dFieldsPanel);

        createDisplayFields(dFieldsPanel); //What really create the fields

        return dFieldsPanel;
    }

    protected void createDisplayFields(JPanel dFieldsPanel) {   



        Object[][] inputFieldObjs = { 
                { dID, "Residential Property ID:" },
                { dFName, "Property Legal Description:" }, 
                { dLName, "Property Address:" },
                { dAddress, "City Quadrant:"},
                { dPCode, "Zoning of Property:"}, 
                { dPhone, "Property Asking Price:" }, 
                { dType, "Building Square Footage:",clientType },
                };


        int row = 0;
        for (Object[] obj : inputFieldObjs){

            GridBagConstraints gbc_Label = createLabelGBConstrain(row,0);
            GridBagConstraints gbc_InputField = createInputFieldGBConstrain(row,1);

            dFieldsPanel.add(createLabel(obj),gbc_Label);

            if(obj.length == 2)
                dFieldsPanel.add(createTextField(obj),gbc_InputField);
            else
                dFieldsPanel.add(createComboBox(obj),gbc_InputField);
            row ++;
        }

    private JLabel createLabel (Object[] objs){
        String labelText = (String) objs[1];
        JLabel label = new JLabel(labelText);
        label.setFont(text);
        return label;
    }

    private JTextField createTextField(Object[] objs){
        JTextField field = (JTextField) objs[0];
        field = new JTextField();
        field.setFont(text);
        field.setColumns(10);
        return field;
    }

    private JComboBox createComboBox(Object[] objs){

        JComboBox field = (JComboBox) objs[0];
        String[] options = (String[]) objs[2];
        field = new JComboBox(options);
        field.setFont(text);
        return field;
    }

    private GridBagConstraints createLabelGBConstrain(int row, int col){

        GridBagConstraints gbc_Label = new GridBagConstraints();
        gbc_Label.anchor = GridBagConstraints.EAST;
        gbc_Label.insets = new Insets(0, 0, 5, 5);
        gbc_Label.gridx = col;
        gbc_Label.gridy = row;
        return gbc_Label;
    }

    private GridBagConstraints createInputFieldGBConstrain(int row, int col){

        GridBagConstraints gbc_InputField = new GridBagConstraints();
        gbc_InputField.anchor = GridBagConstraints.WEST;
        gbc_InputField.insets = new Insets(0, 0, 5, 0);
        gbc_InputField.gridx = 1;
        gbc_InputField.gridy = row;
        return gbc_InputField;
    }

我测试的解决方案: 将 weightx 和 weighty 设置为 0 并将 fields/labels 上的填充设置为 none... 没有用,我只能找到与我的案例远程相关的解决方案

感谢您的帮助=)

编辑:谢谢大家的回答,我的问题真的被camickr的解决方案解决了。我真的不知道那段代码在做什么。它是自动生成的。也感谢关于如何 post 一个更好的问题的评论提示,下次我一定会记住这一点 =)

您必须为要包含额外内容的任何字段设置 weightx 和 weighty 约束 space。您还需要确保为该字段设置了锚点。

确保其余字段的 weightx 和 weighty 设置为零。我通常在开始时将它们设置为零,并且仅在添加最后一个字段时才转换为 1。

我做的第一件事是注释掉以下内容:

/*
    gbl_dFieldsPanel.columnWidths = new int[]{0};
    gbl_dFieldsPanel.rowHeights = new int[] {0};
    gbl_dFieldsPanel.columnWeights = new double[]{Double.MIN_VALUE};
    gbl_dFieldsPanel.rowWeights = new double[]{Double.MIN_VALUE};
*/

这会将所有组件垂直显示在一起。