SWT ScrolledComposite调整大小后显示内容不正确

时间:2020-01-08 13:06:09

标签: java swt

我想要一个ScrolledComposite,其中包含多个复合内容。

如果调整窗口大小,则ScrolledComposite的大小无法正确调整。如果我缩小宽度,它将削减内容的底部,并且不允许用户向下滚动。如果再将宽度增加到更大,它将始终保持滚动条,并允许用户向下滚动太多。

我已经尝试过更新ScrolledComposite的最小高度,就像this问题中的答案一样,但是并没有帮助。我曾尝试按照多个教程并查看示例,但看不到是什么原因造成的。

示例

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class Example {

    public static void main(String[] args) {

    final Display display = new Display();
    final Shell shell = new Shell(display);

    shell.setText("Scrolled Composite Example");

    // Create a scrolled composite with content
    shell.setLayout(new GridLayout(1, true));
    shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Composite scrollParent = new Composite(shell, SWT.NONE);
    scrollParent.setLayout(new FillLayout());
    scrollParent.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(1, 1).create());

    final ScrolledComposite scrollComposite = new ScrolledComposite(scrollParent, SWT.V_SCROLL);

    final Composite scrollContent = new Composite(scrollComposite, SWT.NONE);
    scrollContent.setLayout(new GridLayout(1, true));

    scrollComposite.setContent(scrollContent);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.setExpandVertical(true);

    // Add a composite to the scroll content (A label with lots of text)
    final Composite cell = new Composite(scrollContent, SWT.BORDER);
    cell.setLayout(new GridLayout(4, false));
    cell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    final Label l = new Label(cell, SWT.WRAP);

    l.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    l.setText(StringUtils.repeat('1', 10000));

    // Listen for width changes to update the minimum height
    scrollContent.addListener(SWT.Resize, new Listener() {
        int width = -1;

        @Override
        public void handleEvent(final Event e) {
        int newWidth = scrollContent.getSize().x;
        if (newWidth != width) {
            scrollComposite.setMinHeight(scrollContent.computeSize(newWidth, SWT.DEFAULT).y);
            width = newWidth;
        }
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
    }
}

是什么原因造成的?

1 个答案:

答案 0 :(得分:0)

我设法通过添加ScrolledComposite大小更改的侦听器来解决此问题。

如果调整ScrolledComposite的大小,我将获得工作区的宽度并计算内容所需的最小大小。

scrolledComposite.addListener(SWT.Resize, event -> {
    final int width = scrolledComposite.getClientArea().width;
    scrolledComposite.setMinSize(content.computeSize(width, SWT.DEFAULT));
});