在swt的父复合中,如何在布局布局的情况下获取滚动条?

时间:2019-04-15 15:32:08

标签: eclipse layout 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();
    }
}

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

1 个答案:

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