如何在 swt 中父复合材料中的 formlayout 中获取滚动条?

how to get scrollbar in case of formlayout in parent composite In swt?

我目前正在设计一个我希望有滚动条的布局。我有一个有表单布局的父项(我无法更改)。下面的示例代码重现了相同的场景。

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This class demonstrates ScrolledComposite
 */
public class ScrolledCompositeTest {
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private void createContents(Composite parent) {
        parent.setLayout(new FormLayout());

        // Create the ScrolledComposite to scroll horizontally and vertically
        ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
        // Create a child composite to hold the controls
        Composite child = new Composite(sc, SWT.NONE);
        child.setLayout(new FillLayout());
        sc.setBackground(new Color(parent.getDisplay(), 0,0,0));
        // Create the buttons
        new Button(child, SWT.PUSH).setText("One");
        new Button(child, SWT.PUSH).setText("Two");
        /*
         * // Set the absolute size of the child child.setSize(400, 400);
         */
        // Set the child as the scrolled content of the ScrolledComposite
        sc.setContent(child);

        // Set the minimum size
        sc.setMinSize(500, 500);

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

    public static void main(String[] args) {
        new ScrolledCompositeTest().run();
    }
}

如果我更改父布局以填充或网格化滚动条,expected.Any 这方面的线索将很有帮助。

请将FormData添加到ScrolledComposite

FormData data = new FormData();
data.top = new FormAttachment(0, 5);
data.left = new FormAttachment(0, 5);
data.bottom = new FormAttachment(100, -5);
data.right = new FormAttachment(100, -5);
sc.setLayoutData(data);

public class ScrolledCompositeTest {
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private void createContents(Composite parent) {
        parent.setLayout(new FormLayout());

        // Create the ScrolledComposite to scroll horizontally and vertically
        ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
        sc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_CYAN));

        FormData data = new FormData();
        data.top = new FormAttachment(0, 5);
        data.left = new FormAttachment(0, 5);
        data.bottom = new FormAttachment(100, -5);
        data.right = new FormAttachment(100, -5);
        sc.setLayoutData(data);

        // Create a child composite to hold the controls
        Composite child = new Composite(sc, SWT.NONE);
        child.setLayout(new FillLayout());
        // Create the buttons
        new Button(child, SWT.PUSH).setText("One");
        new Button(child, SWT.PUSH).setText("Two");
        /*
         * // Set the absolute size of the child child.setSize(400, 400);
         */
        // Set the child as the scrolled content of the ScrolledComposite
        sc.setContent(child);

        // Set the minimum size
        sc.setMinSize(500, 500);

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

    public static void main(String[] args) {
        new ScrolledCompositeTest().run();
    }
}

Output :