【问题标题】:SWT ScrolledComposite is displaying content incorrectly after resizingSWT ScrolledComposite 在调整大小后显示内容不正确
【发布时间】:2020-04-26 00:41:25
【问题描述】:

我想要一个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】:

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

    如果调整ScrolledComposite 的大小,我会获取客户区宽度并计算内容所需的最小大小。

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

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2012-10-01
      相关资源
      最近更新 更多