使用 BooleanFieldEditor 时复选框布局的问题

The problem with the layout of checkbox when using BooleanFieldEditor

我正在尝试使用 BooleanFieldEditor 而不是样式为 SWT.CHECK 的 Button。我遇到了复选框布局的问题。

我之前代码的重要部分是:

    Composite projectGroup = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    projectGroup.setLayout(layout);
    projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button checkBox = new Button(projectGroup, SWT.CHECK);
    checkBox.setText(Messages.getString("WizardNewProjectCreationPage.createEmptyProject")); //$NON-NLS-1$
    checkBox.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            Button button = (Button) event.getSource();
            shouldEmptyProjectBeCreated = button.getSelection();
        }
    });
    
    

它给了我这个结果:

在这种情况下,复选框在顶部和左侧有一个小缩进。

我当前代码的重要部分是:

    Composite projectGroup = new Composite(parent, SWT.NONE);
    GridLayoutFactory.fillDefaults().extendedMargins(100, 0, 100, 0).spacing(100, 100).applyTo(projectGroup);
    BooleanFieldEditor emptyProjectCheckbox = new BooleanFieldEditor("createEmptyProject",
            Messages.getString("WizardNewProjectCreationPage.createEmptyProject"), projectGroup);
    //        GridDataFactory.defaultsFor(projectGroup).grab(true, false).span(2, 1).applyTo(projectGroup);
    // emptyProjectCheckbox.fillIntoGrid(projectGroup, 1);
    createEmptyProject = emptyProjectCheckbox.getBooleanValue();
    

无论我在 extendedMargins() 和 spacing() 方法中设置什么值,结果都是一样的 - 复选框严格位于上框架的水平:

如您所见,本例中的缩进比第一张图片上的缩进小。我想制作与第一张图片相同的缩进,并想了解如何管理 BooleanFieldEditor 的复选框相对于其他元素的位置。

使用第二个复合解决了问题:

        Composite projectGroup = new Composite(parent, SWT.NONE);
        GridLayoutFactory.fillDefaults().extendedMargins(5, 0, 5, 0).applyTo(projectGroup);

        //intermediate composite, which needs to work around the problem with layout of checkbox of BooleanFieldEditor
        Composite intermediateComposite = new Composite(projectGroup, SWT.NONE);
        BooleanFieldEditor emptyProjectCheckbox = new BooleanFieldEditor("createEmptyProject",
                Messages.getString("WizardNewProjectCreationPage.createEmptyProject"), intermediateComposite);
        createEmptyProject = emptyProjectCheckbox.getBooleanValue();