簡體   English   中英

如何隱藏SWT復合材料以使其不占用空間?

[英]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#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();
}

隱藏之前:

在此輸入圖像描述

隱藏后:

在此輸入圖像描述

為您的控件定義GridData,然后執行: control.setVisible(false) do gridData.exclude=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM