gpt4 book ai didi

java - Composite 和 ScrolledComposite 占据了不必要的空间

转载 作者:行者123 更新时间:2023-12-02 09:54:03 26 4
gpt4 key购买 nike

以下代码片段在 ScrolledComposite 内生成一个 Composite。我还在里面贴了一个标签。显示垂直滚动条,表明复合 Material 占用了太多空间。

@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");

当我不使用 SWT.FILL 作为 mainComposite 时,子组合不会填充空间。detailsComposite.getVerticalBar().setEnabled(false); 无效,detailsS​​C.getVerticalBar() 返回 null;我怎样才能告诉复合 Material 不要占用超过必要的空间?

最佳答案

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

另外:

  1. 您没有将布局数据设置为 detailsComposite,而是两次设置为 detailsS​​C
  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));

关于java - Composite 和 ScrolledComposite 占据了不必要的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56129547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com