在这种情况下,不要覆盖createButtonsForButtonBar,它已经设置了GridLayout,使得按钮位于右侧.相反,覆盖createButtonBar,这将允许您控制整个复合.
在左侧添加一些东西很容易,然后让SWT为你放置OK / Cancel按钮 – 我建议你这样做,因为你不得不复制“默认”按钮放置位置的逻辑(在Mac上很远的地方)操作系统和最近的GNOME,位于Win32和旧GNOME上的“取消”按钮的左侧.)在这种情况下,您可以设置一个跨越整个按钮栏的复合,然后让SWT在最右侧绘制其按钮栏.
例如:
@Override
protected Control createButtonBar(final Composite parent)
{
final Composite buttonBar = new Composite(parent, SWT.NONE);
final GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = false;
layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
buttonBar.setLayout(layout);
final GridData data = new GridData(SWT.FILL, SWT.BOTTOM, true, false);
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = false;
buttonBar.setLayoutData(data);
buttonBar.setFont(parent.getFont());
// place a button on the left
final Button leftButton = new Button(buttonBar, SWT.PUSH);
leftButton.setText("Left!");
final GridData leftButtonData = new GridData(SWT.LEFT, SWT.CENTER, true, true);
leftButtonData.grabExcessHorizontalSpace = true;
leftButtonData.horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
leftButton.setLayoutData(leftButtonData);
// add the dialog's button bar to the right
final Control buttonControl = super.createButtonBar(buttonBar);
buttonControl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
return buttonBar;
}