[英]How to hide an SWT composite so that it takes no space?
我需要隱藏一個復合材料(以及所有兒童)。 只需設置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);
以下是一些可以滿足您需求的代碼。 我基本上將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();
}
隱藏之前:
隱藏后:
為您的控件定義GridData,然后執行: control.setVisible(false)
do gridData.exclude=true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.