簡體   English   中英

是否應該兩次設置ScrolledComposite內容? 如何正確使用ScrolledComposite?

[英]Should I set ScrolledComposite content twice? How to use ScrolledComposite correctly?

如何正確使用ScrolledComposite?

以下是Snipped166的略微修改:

 import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet166 {

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for(int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0) label.setImage(image1);
        if (i % 3 == 1) label.setImage(image2);
        if (i % 3 == 2) label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = false;
    parent.setLayout(layout);

    scrollComposite.setContent(parent);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(ControlEvent e) {
            Rectangle r = scrollComposite.getClientArea();
            scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
        }
    });

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

我希望它用水平滾動條顯示水平的長行標志。

不幸的是,它沒有繪制滾動條。

我也不明白,為什么這個例子應該這么復雜? 為什么我不能在ScrolledComposite上放一些大東西?

我也不了解setContent()方法的需要,因為內容總是在SWT的構造函數中設置的。

UPDATE

我發現可以手動設置尺寸,然后會出現滾動條。

public class Snippet166_mod01 {

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for(int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0) label.setImage(image1);
        if (i % 3 == 1) label.setImage(image2);
        if (i % 3 == 2) label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = false;
    parent.setLayout(layout);

    // how to calculate actual size?
    parent.setSize(1000, 100);


    // what this is for?
    scrollComposite.setContent(parent);

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

因此,我想知道如何自動設置尺寸? 是否可以將parent控件的大小精確設置為內部51個圖像的寬度?

您還必須告訴ScrolledComposite最小尺寸:

scrolled.setMinSize(group.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scrolled.setExpandHorizontal(true);
scrolled.setExpandVertical(true);

將您的所有孩子都添加到該組后,執行此操作。

暫無
暫無

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

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