Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可见性设置为false后SWT组件重新布置_Java_Layout_Swt - Fatal编程技术网

Java 可见性设置为false后SWT组件重新布置

Java 可见性设置为false后SWT组件重新布置,java,layout,swt,Java,Layout,Swt,假设我有一个列为1的GridLayoutcomposite。(类似于垂直流布局) 我已经将标签1、标签2、标签3添加到该组合中,它们将相应地出现 ---------- Label 1 | Label 2 | Label 3 | ---------- 那么,如果我将标签2的可见性设置为false,标签3是否可以上移以替换标签2?如果标签2的可见性设置回true,标签3将向下移动?一个非常简单的解决方案可以使用GridData::exclude属性。比如说, 代码 import org.ec

假设我有一个列为1的
GridLayout
composite。(类似于垂直流布局)

我已经将标签1、标签2、标签3添加到该组合中,它们将相应地出现

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

那么,如果我将标签2的可见性设置为
false
,标签3是否可以上移以替换标签2?如果标签2的可见性设置回
true
,标签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();
    }
}