Composite 和 ScrolledComposite 比必要的抓取更多 space

Composite and ScrolledComposite grab more space than necessary

以下代码片段在 ScrolledComposite 中生成一个 Composite。我还在里面放了一个标签。显示垂直滚动条,表示复合抓取过多space。

@Override
public void createPartControl(Composite parent) {
    if (this.parent == null) {
        this.parent = parent;
    }
    parent.setLayout(new GridLayout());
    SashForm mainComposite = new SashForm(parent, SWT.NONE);
    mainComposite.setLayout(new GridLayout());
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    mainComposite.setLayoutData(gd);
//  createLeftSide(mainComposite);
    createRightSide(mainComposite);

//  mainComposite.setWeights(new int[] { 1, 4 });
}

问题只出现在createRightSide:

private void createRightSide(Composite mainComposite) {
    ScrolledComposite detailsSC = new ScrolledComposite(mainComposite, SWT.SMOOTH | SWT.BORDER);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    detailsSC.setLayoutData(gd);
    detailsSC.setLayout(new GridLayout(1, true));

    Composite detailsComposite = new Composite(detailsSC, SWT.V_SCROLL);
    detailsComposite.setLayout(new GridLayout(2, false));
    detailsSC.setContent(detailsComposite);
    detailsSC.setExpandHorizontal(true);
    detailsSC.setExpandVertical(true);
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    detailsSC.setLayoutData(gd);

    Label dmyLabel = new Label(detailsComposite, SWT.NONE);
    GridData labelData = new GridData();
    dmyLabel.setLayoutData(labelData);
    dmyLabel.setText("Dummy");

当我不为 mainComposite 使用 SWT.FILL 时,子复合词不会填充 space。 detailsComposite.getVerticalBar().setEnabled(false); 无效,detailsSC.getVerticalBar() returns 为空; 我怎样才能告诉复合材料不要抓取比需要更多的 space?

我不确定我是否能够复制您的确切案例,但对我来说,滚动条似乎出现是因为您将样式 SWT.V_SCROLL 放在 detailsComposite 而不是ScrolledComposite (detailsSC).

还有:

  1. 您没有将布局数据设置为 detailsComposite,而是两次设置为 detailsSC
  2. 您没有设置内容的最小大小以使 ScrolledComposite 知道何时显示滚动条

此代码应该有效:

private void createRightSide(Composite mainComposite) {
    ScrolledComposite detailsSC = new ScrolledComposite(mainComposite, SWT.SMOOTH | SWT.BORDER | SWT.V_SCROLL);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    detailsSC.setLayoutData(gd);
    detailsSC.setLayout(new GridLayout(1, true));
    detailsSC.setExpandHorizontal(true);
    detailsSC.setExpandVertical(true);

    Composite detailsComposite = new Composite(detailsSC, SWT.NONE);
    detailsComposite.setLayout(new GridLayout(2, false));
    GridData gdd = new GridData(SWT.FILL, SWT.FILL, true, true);
    detailsComposite.setLayoutData(gdd);

    Label dmyLabel = new Label(detailsComposite, SWT.NONE);
    GridData labelData = new GridData();
    dmyLabel.setLayoutData(labelData);
    dmyLabel.setText("Dummy");

    detailsSC.setContent(detailsComposite);       
    detailsSC.setMinSize(detailsComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));