无法将 GridLayout 设置为复合容器

Unable to set GridLayout to a Composite container

我无法在 Eclipse Neon 3 的插件项目中将 GridLayout 设置为复合容器。

我正在编写的用于设置容器布局的代码是:

private Composite container;

@Override
public void createControl(Composite parent) {
    container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(1,2);
    container.setLayout(layout);
}

setLayout() 方法抛出错误,说明如下:

The method setLayout(Layout) in the type Composite is not applicable for the arguments (GridLayout)

您正在导入错误 GridLayout class。

你需要org.eclipse.swt.layout.GridLayoutsetLayout 只接受扩展 org.eclipse.swt.widgets.Layout 的布局。普通的 SWT 应用程序不会有任何 java.awt.xxx 导入。

请注意,SWT GridLayout 没有带有行和列参数的构造函数。您只需指定列数,布局就会计算出行数。类似于:

GridLayout layout = new GridLayout(2, false); // 2 columns, different widths
container.setLayout(layout);