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

Ann*_* W. 1 java eclipse swt

我是SWT的新手,正在尝试创建包含多个嵌套Composites 的页面。我在使用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;
  }

}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我收到以下异常,抱怨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)
Run Code Online (Sandbox Code Playgroud)

如果将的布局类型更改stackComposite1FillLayout,则会收到类似的错误,抱怨使用GridData代替FillData。但是,当我gridComposite从代码中完全删除时,它可以正常工作。

我觉得我在这里错过了一些非常关键的事情。据我了解,布局仅会影响a Composite排列包含在其内部的UI元素的方式。为什么当我明确地将其完全设置为另一种布局类型时,嵌套的gridComposite强制其父代stackComposite1也要使用GridData

gre*_*449 5

你有:

Composite stackComposite1 = new Composite(pageComposite, SWT.NONE);
stackComposite1.setLayout(new RowLayout(SWT.VERTICAL));
Run Code Online (Sandbox Code Playgroud)

所以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));
Run Code Online (Sandbox Code Playgroud)

问题行在这里是:

gridComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Run Code Online (Sandbox Code Playgroud)

设置setLayoutData为Composite布局时使用的布局数据-此处的父级使用的是行布局,而不是网格布局。您应该在这里使用。stackComposite1RowData

  • 那么我是否理解,在 Composite 上设置布局定义了其子项在其自身内的排列方式,但在 Composite 上设置布局 **data** 定义了 Composite 在其父项中如何排列自身? (2认同)