如何隐藏SWT复合体,使其不占用空间?

2022-09-01 20:51:55

我需要隐藏一个复合物(以及里面的所有孩子)。只需设置即可保留复合的空间。setVisible(false)

Composite outer = new Composite(parent, SWT.NONE);      
outer.setLayout(new GridLayout(1,false));
outer.setLayoutData(new GridData(GridData.FILL_BOTH) );

Composite compToHide = new MyComposite(outer, SWT.NONE);        
compToHide.setLayout(new GridLayout());
compToHide.setVisible(false);

答案 1

下面是一些执行所需操作的代码。我基本上结合使用来隐藏/取消隐藏:GridData#excludeControl#setVisible(boolean)Composite

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, true));

    Button hideButton = new Button(shell, SWT.PUSH);
    hideButton.setText("Toggle");

    final Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(new GridLayout(3, false));

    final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    content.setLayoutData(data);

    for(int i = 0; i < 10; i++)
    {
        new Label(content, SWT.NONE).setText("Label " + i);
    }

    hideButton.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            data.exclude = !data.exclude;
            content.setVisible(!data.exclude);
            content.getParent().pack();
        }
    });

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

隐藏前:

enter image description here

隐藏后:

enter image description here


答案 2

为控件定义 GridData,然后执行以下操作:control.setVisible(false)gridData.exclude=true


推荐