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

问题描述 投票:1回答:1

我想要一个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();
    }
}

是什么原因造成的?

java swt
1个回答
0
投票

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

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

scrolledComposite.addListener(SWT.Resize, event -> {
    final int width = scrolledComposite.getClientArea().width;
    scrolledComposite.setMinSize(content.computeSize(width, SWT.DEFAULT));
});
© www.soinside.com 2019 - 2024. All rights reserved.