如何在父组合中的formlayout情况下获取滚动条在swt中?

问题描述 投票:0回答:1

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

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();
    }
}

如果我将父布局更改为填充或网格,则滚动条按预期工作。任何关于此的线索都将有所帮助。

eclipse layout swt
1个回答
3
投票

请将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();
    }
}

输出:

Output1 Output2

© www.soinside.com 2019 - 2024. All rights reserved.