是否可以直接向ScrolledComposite添加Button?

时间:2016-07-06 19:28:55

标签: java swt scrolledcomposite

ScrolledComposite延伸Composite。那么是否可以直接将Button添加到滚动的复合材料而不需要其他复合材料?

1 个答案:

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

在调整大小之前:

enter image description here

调整大小后:

enter image description here

请记住以ScrolledComposite#setContent(Control)作为参数调用Button