并非GridBagLayout行中显示的所有项目

时间:2022-09-17 14:36:48

I am designing a panel to display items using a GridBagLayout. The result I am looking for is something like this:

我正在设计一个使用GridBagLayout显示项目的面板。我正在寻找的结果是这样的:

并非GridBagLayout行中显示的所有项目

However, currently I am seeing the following:

但是,目前我看到以下情况:

并非GridBagLayout行中显示的所有项目

I then initialise my panel and set the layout:

然后我初始化我的面板并设置布局:

JPanel surveyDetailsFieldsPane = new JPanel();  
surveyDetailsFieldsPane.setMinimumSize(new Dimension(0, 300));
surveyDetailsFieldsPane.setPreferredSize(new Dimension(410, 300));
GridBagLayout detailsGridBagLayout = new GridBagLayout();
surveyDetailsFieldsPane.setLayout(detailsGridBagLayout);
surveyDetailsFieldsPane.setBorder(new EmptyBorder(STANDARD_BORDER,
            STANDARD_BORDER,
            STANDARD_BORDER,
            STANDARD_BORDER));

I initialise my components to add to the grid:

我初始化我的组件以添加到网格:

JLabel jobNameLbl.setText("Job Name:");
JTextField jobNameTF = new JTextField(20);

JLabel jobNumLbl.setText("Job Number:");
JTextField jobNumTF = new JTextField(12);

Then I add components to the grid using the addComponentToSurveyDetailsGrid method:

然后我使用addComponentToSurveyDetailsGrid方法将组件添加到网格中:

 // Row 1
 addComponentToSurveyDetailsGrid(jobNameLbl, 0, 0, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
 addComponentToSurveyDetailsGrid(jobNameTF, 1, 0, 1, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
 // Row 2
 addComponentToSurveyDetailsGrid(jobNumLbl, 0, 1, 1, 1, 10, 0, GridBagConstraints.NONE, GridBagConstraints.WEST); 
 addComponentToSurveyDetailsGrid(jobNumTF, 1, 1, 0, 1, 40, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
 addComponentToSurveyDetailsGrid(dateLbl, 4, 1, 1, 1, 10, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
 addComponentToSurveyDetailsGrid(dateTF, 5, 1, 0, 1, 40, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);

The method used is shown below:

使用的方法如下所示:

private void addComponentToSurveyDetailsGrid(Component component, int gridX, int gridY,
                            int gridWidth, int gridHeight, int weightX, 
                            int weightY, int fill, int anchor) {

    GridBagConstraints constraint = new GridBagConstraints();
    constraint.gridx = gridX;
    constraint.gridy = gridY;
    constraint.gridwidth = gridWidth;
    constraint.gridheight = gridHeight;
    constraint.weightx = weightX;
    constraint.weighty = weightY;       
    constraint.fill = fill;
    constraint.anchor = anchor;
    constraint.insets = new Insets(Dimens.SMALL_BORDER, 
                                Dimens.SMALL_BORDER, 
                                Dimens.SMALL_BORDER, 
                                Dimens.SMALL_BORDER);
    detailsGridBagLayout.setConstraints(component, constraint);
    surveyDetailsFieldsPane.add(component);
}

Please let me know what I have done wrong here!

请让我知道我在这里做错了什么!

1 个解决方案

#1


3  

I doubt it will solve your problem but:

我怀疑它会解决你的问题但是:

 addComponentToSurveyDetailsGrid(jobNumLbl, 0, 1, 1, 1, 10, 0, ...
 addComponentToSurveyDetailsGrid(jobNumTF, 1, 1, 0, 1, 40, 0, ...
 addComponentToSurveyDetailsGrid(dateLbl, 4, 1, 1, 1, 10, 0, ...
 addComponentToSurveyDetailsGrid(dateTF, 5, 1, 0, 1, 40, 0, ...

Your gridx values are 0, 1, 4, 5.

您的gridx值为0,1,4,5。

You can't just try to add a component to grid 4. The GridBagLayout doesn't magically add components to grids 2 and 3. So you need to add the components to grid 2 and 3.

您不能只尝试将组件添加到网格4. GridBagLayout不会奇怪地将组件添加到网格2和3.因此您需要将组件添加到网格2和3。

If you want the jobname text field to occupy the space of the 3 components under it, then you need to specify a gridwidth value of 3 when you add the jobname text field.

如果希望作业名文本字段占用其下3个组件的空间,则需要在添加作业名文本字段时指定网格宽度值3。

Read the section from the Swing tutorial on How to Use GridBagLayout. The tutorial will explain how the gridwidth parameter works and provides a working example for you to download and test.

阅读Swing教程中有关如何使用GridBagLayout的部分。本教程将解释gridwidth参数的工作原理,并为您提供下载和测试的工作示例。

#1


3  

I doubt it will solve your problem but:

我怀疑它会解决你的问题但是:

 addComponentToSurveyDetailsGrid(jobNumLbl, 0, 1, 1, 1, 10, 0, ...
 addComponentToSurveyDetailsGrid(jobNumTF, 1, 1, 0, 1, 40, 0, ...
 addComponentToSurveyDetailsGrid(dateLbl, 4, 1, 1, 1, 10, 0, ...
 addComponentToSurveyDetailsGrid(dateTF, 5, 1, 0, 1, 40, 0, ...

Your gridx values are 0, 1, 4, 5.

您的gridx值为0,1,4,5。

You can't just try to add a component to grid 4. The GridBagLayout doesn't magically add components to grids 2 and 3. So you need to add the components to grid 2 and 3.

您不能只尝试将组件添加到网格4. GridBagLayout不会奇怪地将组件添加到网格2和3.因此您需要将组件添加到网格2和3。

If you want the jobname text field to occupy the space of the 3 components under it, then you need to specify a gridwidth value of 3 when you add the jobname text field.

如果希望作业名文本字段占用其下3个组件的空间,则需要在添加作业名文本字段时指定网格宽度值3。

Read the section from the Swing tutorial on How to Use GridBagLayout. The tutorial will explain how the gridwidth parameter works and provides a working example for you to download and test.

阅读Swing教程中有关如何使用GridBagLayout的部分。本教程将解释gridwidth参数的工作原理,并为您提供下载和测试的工作示例。