如何在 SWT 中将标签项放在 rowLayout 上?

How to put label item on rowLayout in SWT?

我想在行布局中放置一些项目。但是我犯了一个错误。我的错误图片在这里:

我的示例代码如下:

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

    // design filter composite layout
    Composite filterComposite = new Composite(parent, SWT.NONE);

    RowLayout filterCompositeLayout = new RowLayout();
    filterCompositeLayout.wrap = true;
    filterCompositeLayout.pack = false;
    filterCompositeLayout.justify = true;
    filterCompositeLayout.type = SWT.HORIZONTAL;
    filterComposite.setLayout(filterCompositeLayout);

    // design filter composite
    Label lbl_type = new Label(filterComposite, SWT.BORDER);
    lbl_type.setText("Type :");

Combo cmb_type = new Combo(filterComposite, SWT.BORDER);
    cmb_type.setText("-- choose --");

    Label lbl_severity = new Label(filterComposite, SWT.BORDER);
    lbl_severity.setText("Severity :");

    Combo cmb_severity = new Combo(filterComposite, SWT.BORDER);
    cmb_severity.setText("-- choose --");

    Label lbl_startDate = new Label(filterComposite, SWT.BORDER);
    lbl_startDate.setText("Start Date : ");

    DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
    dateTimeStart.setLayoutData(new GridData(SWT.NONE, SWT.NONE, true, true));

DataTime 行有错误。有人可以给一些建议吗?谢谢。

您不能将 GridData 设置为放置在 RowLayout 中的组件。您应该改用 RowData:

    DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
    RowData rd = new RowData();
    rd.width = 123;
    rd.height = 23;
    dateTimeStart.setLayoutData(rd);

您正在尝试为 dateTimeStart 设置 GridData 布局数据。然而 Composite filterComposite 有 RowLayout 并且这个 Layout 需要 RowData 布局数据。

示例:

DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
dateTimeStart.setLayoutData(new RowData (width, height)); //set proper height and width according to your UI expectations