如何使用SWT更改组件的父级?

6

我的窗口应该允许两种不同的布局(这是一个简单的例子,以更好地说明它),例如:

+-------------+-------------+-------------+
| Component 1 | Component 2 | Component 3 |
|             |             |             |
|             |             |             |
|             |             |             |
|             |             |             |
+-------------+-------------+-------------+

并且

+-------------+---------------------------+
| Component 1 | Component 2               |
|             |                           |
|             +---------------------------+
|             | Component 3               |
|             |                           |
+-------------+---------------------------+

用户可以通过菜单项在两者之间切换。

使用SWT创建组件时需要提供父级。但是我们需要(1)重用组件并将它们放置在不同的父级中(类似于停靠框架)。那么如何在SWT中实现这一点呢?

1个回答

7
您可以通过更改组件的父级来实现此操作。
如果底层操作系统支持,setParent()会更改控件的父级。然后,您可以使用layout()重新布局组合,以便更改生效。
假设您有三个控件:
- 包含垂直控件的复合控件c1 - 包含水平控件的复合控件c2 - 一个标签lbl - 一个按钮btn 以下是代码:
public class ControlSwitcher {
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        GridLayout gl = new GridLayout();
        gl.marginWidth = gl.marginHeight = 20;
        shell.setLayout(gl);

        final Composite c1 = new Composite(shell, SWT.NONE);
        c1.setBackground(new Color(display, 255, 160, 160));
        RowLayout layout = new RowLayout(SWT.VERTICAL);
        c1.setLayout(layout);

        final Composite c2 = new Composite(c1, SWT.NONE);
        c2.setBackground(new Color(display, 160, 255, 160));
        c2.setLayout(new RowLayout());

        final Label lbl = new Label(c2, SWT.NORMAL);
        lbl.setText("Hello world");

        final Button btn = new Button(c2, SWT.PUSH);
        btn.setText("Switch");
        btn.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                Composite target;
                if (btn.getParent().equals(c2)) {
                    target = c1;
                } else {
                    target = c2;
                }
                boolean success = btn.setParent(target);
                if (success) {
                    target.pack();
                    shell.pack();
                } else {
                    throw new RuntimeException("Not supported by this platform");
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
            }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

你知道在Windows、OS X和Linux/GTK上是否支持设置父级吗? - Mot
不行,我只在Vista上测试过。我可以在周一在办公室为您检查。 - Paul Lammertsma
我已在Linux上测试过它,它可以正常工作(假设安装了GTK+,这是Ubuntu/Kubuntu/Xubuntu的默认设置)。 - Paul Lammertsma
我还没有在OS X上测试过它,但我做过类似的事情,并且相当确定在该平台上也不会有问题。 - Paul Lammertsma
小提示:当以这种方式更改布局时,活动控件会失去焦点。您可能需要在layout()之后执行setFocus() - Paul Lammertsma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接