【问题标题】:Should I set ScrolledComposite content twice? How to use ScrolledComposite correctly?我应该设置两次 ScrolledComposite 内容吗?如何正确使用 ScrolledComposite?
【发布时间】:2014-01-22 14:33:37
【问题描述】:

如何正确使用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 的构造函数中设置。

更新

我发现,我可以手动设置大小,然后会出现卷轴。

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张图片的宽度?

【问题讨论】:

  • 我的回答解决了你的问题吗?

标签: java swt scrolledcomposite


【解决方案1】:

你还必须告诉ScrolledComposite它的最小尺寸:

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

将所有孩子都添加到组后执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多