我是SWT的新手,正在尝试创建包含多个嵌套Composite
的页面。我在使用嵌套在其他布局类型的GridLayout
中的Composite
时遇到了问题。
这是一些简化的模拟代码,它们说明了我要完成的工作以及遇到的错误:
package layouts_test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
public class LayoutsTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// Composite that contains all nested Composites
Composite pageComposite = new Composite(shell, SWT.NONE);
final StackLayout stackLayout = new StackLayout();
pageComposite.setLayout(stackLayout);
// Construct some Composites to stack on the pageComposite
Composite stackComposite1 = new Composite(pageComposite, SWT.NONE);
stackComposite1.setLayout(new RowLayout(SWT.VERTICAL));
Composite stackComposite2 = new Composite(pageComposite, SWT.NONE);
stackComposite2.setLayout(new FillLayout());
// Construct a grid composite to go into stackComposite1
Composite gridComposite = new Composite(stackComposite1, SWT.NONE);
gridComposite.setLayout(new GridLayout(3, true));
gridComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
gridComposite.layout();
// Arbitrarily set stackComposite1 on top
stackLayout.topControl = stackComposite1;
stackComposite1.layout();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
return;
}
}
当我运行此代码时,出现以下异常,抱怨
stackComposite1
显然正在使用GridData
,而应该使用RowData
进行布局:Exception in thread "main" java.lang.ClassCastException: org.eclipse.swt.layout.GridData cannot be cast to org.eclipse.swt.layout.RowData
at org.eclipse.swt.layout.RowLayout.layoutVertical(RowLayout.java:367)
at org.eclipse.swt.layout.RowLayout.layout(RowLayout.java:239)
at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1291)
at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1277)
at org.eclipse.swt.widgets.Composite.layout(Composite.java:666)
at org.eclipse.swt.widgets.Composite.layout(Composite.java:624)
at org.eclipse.swt.widgets.Composite.layout(Composite.java:587)
at layouts_test.LayoutsTest.main(LayoutsTest.java:39)
如果将
stackComposite1
的布局类型更改为FillLayout
,则会收到类似的错误,抱怨使用GridData
而不是FillData
。但是,当我从代码中完全删除gridComposite
时,它可以正常工作。我觉得我在这里错过了一些非常关键的事情。据我了解,布局仅影响
Composite
排列自身中包含的UI元素的方式。为什么当我明确地将其完全设置为另一种布局类型时,嵌套的gridComposite
强制其父级stackComposite1
也使用GridData
? 最佳答案
你有:
Composite stackComposite1 = new Composite(pageComposite, SWT.NONE);
stackComposite1.setLayout(new RowLayout(SWT.VERTICAL));
因此
stackComposite1
正在使用RowLayout
。然后您有:
Composite gridComposite = new Composite(stackComposite1, SWT.NONE);
gridComposite.setLayout(new GridLayout(3, true));
gridComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
问题行在这里是:
gridComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
setLayoutData
设置的布局数据由父级Composite布局使用-此处的父级为stackComposite1
,它使用的是行布局,而不是网格布局。您应在此处使用RowData
。