在SWT\RCP中是否存在类似cardLayout的布局

Abh*_*ary 5 java layout swt

我想在SWT中使用布局,其作用类似于Swing的car​​dLayout.我的主要要求就像我有一个复选框并有2个SWT组.

并且基于复选框选择和取消选择需要分别在同一位置显示SWT组.

基于复选框状态,一次只能看到一个组.如何实现同样的目标.

Baz*_*Baz 8

你可以使用a StackLayout来实现你想要的:

private static boolean buttonOnTop = true;

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");

    shell.setLayout(new FillLayout());

    Button switchButton = new Button(shell, SWT.NONE);
    switchButton.setText("Switch");

    final StackLayout layout = new StackLayout();

    final Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(layout);

    final Button button = new Button(content, SWT.PUSH);
    button.setText("Button");

    final Label label = new Label(content, SWT.NONE);
    label.setText("Label");

    layout.topControl = button;

    switchButton.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            layout.topControl = (buttonOnTop) ? label : button;
            content.layout();

            buttonOnTop = !buttonOnTop;
        }
    });

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Run Code Online (Sandbox Code Playgroud)

通过设置,StackLayout#topControl您可以"移动" Control到顶部.