java 如何隐藏 SWT 复合材料以使其不占用空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17513210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide an SWT composite so that it takes no space?
提问by yuris
I need to hide a composite (and all children inside). Just setting setVisible(false)
will keep the space of the composite.
我需要隐藏一个组合(以及里面的所有孩子)。只需设置即可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);
回答by Baz
Here is some code that does what you want. I basically use GridData#exclude
in combination with Control#setVisible(boolean)
to hide/unhide the Composite
:
这是一些可以执行您想要的操作的代码。我基本上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();
}
Before hiding:
隐藏前:
After hiding:
隐藏后:
回答by ACV
Define a GridData for your control and then after you do:
control.setVisible(false)
do gridData.exclude=true
定义的GridData你的控制和你再经过:
control.setVisible(false)
做gridData.exclude=true