java - 是否可以直接将 Button 添加到 ScrolledComposite 中?

标签 java swt scrolledcomposite

ScrolledComposite 扩展了Composite。那么是否可以直接将 Button 添加到滚动的复合 Material 中,而无需在其中添加其他复合 Material ?

最佳答案

当然有可能:

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

只需记住以 Button 作为参数调用 ScrolledComposite#setContent(Control) 即可。

关于java - 是否可以直接将 Button 添加到 ScrolledComposite 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38232256/

相关文章:

java - 在 java swt 中向复合布局添加滚动条不起作用

size - SWT 复合 Material 最大尺寸

java - ScrolledComposite 不通过鼠标滚轮滚动

java - 将旧的 zookeeper znode/data 复制/迁移到新的 zookeeper

java - Safari 5/iOS,WebSocket 握手有时有效

java - Jenkins 可以在 slave 日志上自定义插件到 master 吗?

java - SWT - ScrolledComposite 内多行文本字段的 computingSize

java - 评估 Math.tan(Math.acos(0d))

java - SWT Composite 在 3.6.1 中需要 pack()

Java 应用程序 SWT : Update UI on thread finished