0

ScrolledComposite延伸Composite。那么是否可以直接添加Button到滚动的合成中而无需在其中添加另一个合成?

4

1 回答 1

2

当然有可能:

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

    ScrolledComposite sc = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);

    Button button = new Button(sc, SWT.NONE);
    button.setText("Hello! This is a button with a lot of text...");

    sc.setContent(button);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(button.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    shell.pack();
    shell.open();

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

调整大小之前:

在此处输入图像描述

调整大小后:

在此处输入图像描述

只记得ScrolledComposite#setContent(Control)使用Buttonas 参数调用。

于 2016-07-06T19:43:56.323 回答