SWT布局创建

时间:2011-12-15 15:53:37

标签: eclipse swt

我很擅长使用SWT创建布局。这是我必须创造的 enter image description here

到目前为止,这是我的尝试: enter image description here

代码:

   private void createContents(final Shell shell) {
    shell.setLayout(new GridLayout(2,true));

    /// Side Panel
    Composite sideComposite = new Composite(shell, SWT.NONE);
    sideComposite.setLayout(new RowLayout(SWT.VERTICAL | SWT.BORDER ));
    Label codeLabel = new Label(sideComposite, SWT.NONE);
    codeLabel.setText("Folders");

    // Create list of folders
    Composite foldersComposite = new Composite(sideComposite, SWT.NONE);
    foldersComposite.setLayout(new GridLayout(2,true));
    createFoldersComposite(foldersComposite);

    // Create tags
    Composite tagsComposite = new Composite(sideComposite, SWT.NONE);
    tagsComposite.setLayout(new RowLayout());
    createTagsComposte(tagsComposite);

    /// .Side Panel

    /// Main Panel
    Composite mainComposite = new Composite(shell, SWT.NONE);
    mainComposite.setLayout(new RowLayout(SWT.VERTICAL));

    // Create search field
    Composite searchComposite = new Composite(mainComposite, SWT.NONE);
    searchComposite.setLayout(new GridLayout(2,true));

    Label searchLabel = new Label(searchComposite, SWT.NONE);
    searchLabel.setText("Search");
    GridData gridData = new GridData();
    gridData.horizontalSpan = 1;
    searchLabel.setLayoutData(gridData);

    Text searchText = new Text(mainComposite, SWT.BORDER | SWT.V_SCROLL);
    searchLabel.setLayoutData(gridData);

    // Create search result
    Composite resultComposite = new Composite(mainComposite, SWT.BORDER);
    resultComposite.setLayout(new GridLayout(2,true));
    createResultComposite(resultComposite);

    // Code Review
    Text codeText = new Text(mainComposite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER );
    codeText.setSize(500, 500);


    /// .Main Panel
}

问题:如何修复现有代码以获得我想要的确切布局?

1 个答案:

答案 0 :(得分:1)

将其拆分为单独的小部件可能会有所帮助,而不是让单个控件试图同时处理所有内容的布局。这允许您正确地确定每个单独组件上的布局,然后一次构建它们以获得所需的整体布局。

这在您的示例代码中尤为明显 - 您创建了一个名为Composite的{​​{1}},其中包含两列searchComposite,并且您正在添加GridLayout复合材料。但是您要将searchLabel添加到searchText,我认为这不是您的意图。

相反,我会创建一个mainComposite,其中包含SearchControlGridLayout的两列LabelText上的GridData应该Text来填充控件的其余部分。

我建议您更熟悉grabExcessHorizontalSpaceGridLayout。不要害怕尝试。例如,您始终将GridData传递给true构造函数,这将构成列匹配宽度。虽然这是完全合理的,但它与您绘制的布局不匹配,其中某些列比彼此更宽。另外,我建议查看GridLayout。它会将所有GridDataFactory摆弄成一个单行。