如何在 SWT 中从组合中添加一个组?

How to add a group out of a composite in SWT?

我在父级中有一个过滤器组合。我的示例图片在这里:

我想从这个过滤器组合中添加一个组。我的示例代码如下:

// create parent layout
    GridLayout parentLayout = new GridLayout(1, true);
    parent.setLayout(parentLayout);         

    // design filter composite layout
    Group grp = new Group(parent, SWT.NONE);

    RowLayout grdLay = new RowLayout();

    GridData grdData = new GridData();

    grp.setLayoutData(grdData);

    grp.setLayout(grdLay);

    Composite grpComp = new Composite(grp,SWT.NONE);

    GridData gdData = new GridData();       

    RowLayout grplayout = new RowLayout();

    grpComp.setLayout(grplayout);

    grpComp.setLayoutData(gdData);

    Composite filterComposite = new Composite(grpComp, SWT.NONE);
    GridData gd_filterComposite = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
    gd_filterComposite.widthHint = 1611;
    gd_filterComposite.heightHint = 42;
    filterComposite.setLayoutData(gd_filterComposite);

    RowLayout filterCompositeLayout = new RowLayout();
    filterCompositeLayout.type = SWT.VERTICAL;
    filterCompositeLayout.marginHeight = 5;
    filterCompositeLayout.center = true;
    filterCompositeLayout.fill = true;
    filterCompositeLayout.justify = true;
    filterCompositeLayout.wrap = true;
    filterComposite.setLayout(filterCompositeLayout);

    filterCompositeLayout.spacing = 20;

但是我有一个错误。我的错误图片在这里:

我怎样才能做到这一点?有人可以给一些建议吗?谢谢。

对于作为 RowLayout 一部分的控件,布局数据必须是 RowData。

所以

RowData gdData = new RowData();

因为 grpCompRowLayout 集的一部分 grp

同样,filterComposite 布局数据应为 RowData

您必须始终将控件的布局数据与包含该控件的 Composite 的布局相匹配。