我是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
,当我明确地将其设置为另一种布局类型时?
答案 0 :(得分:4)
你有:
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
设置的布局数据由父复合布局使用 - 此处的父级是stackComposite1
,它使用行布局而不是网格布局。你应该在这里使用RowData
。