0

使用选项卡窗口并为外壳提供固定大小,如果空间不足,我如何使内容(即整个外壳)可滚动?这是外壳、选项卡主机还是单个选项卡的设置?

4

1 回答 1

2

使用一个ScrolledComposite包含你全部内容的。这样,如果没有足够的空间来显示它,它将滚动。

以下代码应该让您了解它是如何工作的:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create the ScrolledComposite to scroll horizontally and vertically
    ScrolledComposite scrolledComp = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);

    // Create a child composite for your content
    Composite content = new Composite(scrolledComp, SWT.NONE);
    content.setLayout(new FillLayout());

    // Create some content
    new Button(content, SWT.PUSH).setText("Button1");
    new Button(content, SWT.PUSH).setText("Button2");

    // add content to scrolled composite
    scrolledComp.setContent(content);

    // Set the minimum size (in this case way too large)
    scrolledComp.setMinSize(400, 400);

    // Expand both horizontally and vertically
    scrolledComp.setExpandHorizontal(true);
    scrolledComp.setExpandVertical(true);

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

运行它,减小窗口大小,您将看到滚动条。

于 2012-09-10T10:16:03.710 回答