【问题标题】:Nested SWT Composites with conflicting GridLayout具有冲突 GridLayout 的嵌套 SWT 组合
【发布时间】:2015-03-18 16:12:25
【问题描述】:

我是 SWT 新手,正在尝试创建一个包含多个嵌套 Composites 的页面。我在使用嵌套在其他布局类型的Composites 中的GridLayout 时遇到了问题。

这是一些简化的模拟代码,说明了我要完成的工作,以及我遇到的错误:

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,而我显然是用另一种布局类型设置它?

【问题讨论】:

    标签: java eclipse swt


    【解决方案1】:

    你有:

    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 设置的布局数据被 parent 复合布局使用 - 这里的父级是 stackComposite1,它使用的是行布局而不是网格布局。你应该在这里使用RowData。

    【讨论】:

    • 那么我是否理解,在 Composite 上设置布局定义了其子项在其内部的排列方式,但在 Composite 上设置布局 data 定义了 Composite 自身的排列方式在其父级内?
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2020-09-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多