簡體   English   中英

SWT ScrolledComposite 在調整大小后顯示內容不正確

[英]SWT ScrolledComposite is displaying content incorrectly after resizing

我想要一個包含多個復合材料的ScrolledComposite

如果調整窗口大小,則ScrolledComposite無法正確調整大小。 如果我縮小寬度,它將切掉內容的底部,並且不允許用戶向下滾動到足夠的程度。 如果我將寬度增加到更大,它將始終保持滾動條並允許用戶向下滾動太多。

我已經嘗試像這個問題中的答案一樣更新ScrolledComposite的最小高度,但它沒有幫助。 我試過遵循多個教程並查看示例,但我看不出是什么導致了這種情況。

例子

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();
    }
}

這是什么原因造成的?

我設法通過為ScrolledComposite大小更改添加偵聽器來解決此問題。

如果調整ScrolledComposite的大小,我將獲得客戶區寬度並計算內容所需的最小大小。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM