具有冲突的GridLayout的嵌套SWT复合材料

时间:2015-03-18 16:12:26

标签: java eclipse swt

我是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,当我明确地将其设置为另一种布局类型时?

1 个答案:

答案 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