24

我需要隐藏一个复合材料(以及里面的所有孩子)。只需设置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);
4

2 回答 2

28

这是一些可以满足您需求的代码。我基本上GridData#exclude结合使用Control#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();
}

隐藏前:

在此处输入图像描述

隐藏后:

在此处输入图像描述

于 2013-07-07T15:01:36.060 回答
5

为您的控件定义一个 GridData,然后在您执行此操作之后: control.setVisible(false)执行gridData.exclude=true

于 2018-03-12T11:26:02.350 回答