SWT / JFace如何使标签窗口可滚动

时间:2012-09-10 10:10:17

标签: java tabs swt jface

使用选项卡窗口并为shell提供固定大小,如果没有足够的空间,我如何使内容 - 意味着整个shell - 可滚动?这是shell,标签主机还是各个标签的设置?

1 个答案:

答案 0 :(得分: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();
}

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