java – SWT组件在可见性设置为false后重新发送

让我们说如果我有一个GridLayout复合,列= 1(类似垂直流布局)
我已经将Label 1,Label 2,Label 3添加到此复合材料中,并会相应出现.

----------
Label 1  |
Label 2  |
Label 3  |
----------

那么可能的是,如果我将Label 2的可见性设置为false,Label 3可以向上移动以替换Label 2?如果Label 2的可见性设置为true,Label 3将向下移动?

一个非常简单的解决方案可以使用GridData :: exclude属性.例如,

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class HideLabel 
{
    public static void main(String[] args)
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
        shell.setText("Hide Label");

        Label label = new Label(shell, SWT.NONE);
        label.setText("Label 1");

        final Label bHidden = new Label(shell, SWT.NONE);
        bHidden.setText("Label 2");
        GridData data = new GridData();
        data.exclude = false;
        data.horizontalAlignment = SWT.FILL;
        bHidden.setLayoutData(data);

        label = new Label(shell, SWT.NONE);
        label.setText("Label 3");

        Button button = new Button(shell, SWT.CHECK);
        button.setText("hide");
        button.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                Button b = (Button) e.widget;
                GridData data = (GridData) bHidden.getLayoutData();
                data.exclude = b.getSelection();
                bHidden.setVisible(!data.exclude);
                shell.layout(false);
            }
        });
        shell.setSize(200, 200);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
翻译自:https://stackoverflow.com/questions/12189543/swt-components-relayout-after-visibility-set-to-false

转载注明原文:java – SWT组件在可见性设置为false后重新发送