简体   繁体   English

是否应该两次设置ScrolledComposite内容? 如何正确使用ScrolledComposite?

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

How to use ScrolledComposite correctly? 如何正确使用ScrolledComposite?

The following is slightly modified Snipped166: 以下是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();
}
}

I would like it to display long horizontal row of signs with horizontal scroll bars. 我希望它用水平滚动条显示水平的长行标志。

Unfortunately it doesn't draw scroll bars. 不幸的是,它没有绘制滚动条。

Also I don't understand, why this example should be so complex? 我也不明白,为什么这个例子应该这么复杂? Why can't I just put something big onto ScrolledComposite and roll? 为什么我不能在ScrolledComposite上放一些大东西?

Also I don't understand the need of setContent() method since content is always set in constructor in SWT. 我也不了解setContent()方法的需要,因为内容总是在SWT的构造函数中设置的。

UPDATE UPDATE

I found, that I can set size manually and scrolls will appear then. 我发现可以手动设置尺寸,然后会出现滚动条。

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

So, I wonder, how to set size automatically? 因此,我想知道如何自动设置尺寸? Is it possible to set the size of parent control exactly to the width of 51 images inside? 是否可以将parent控件的大小精确设置为内部51个图像的宽度?

You also have to tell the ScrolledComposite its min-size: 您还必须告诉ScrolledComposite最小尺寸:

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

Do this after adding all your children to the group. 将您的所有孩子都添加到该组后,执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM