SWT Shell会根据孩子的大小调整大小

GGr*_*rec 5 java swt jface eclipse-rcp

我在这个Composite画布上工作,Composite可以添加和删除其他s.

我对整个布局概念仍处于迷雾中的理解.

当孩子被添加到容器中时,考虑到容器中有一个GridData填充父级的事实,父母是否也应该知道孩子的大小调整?由于shell(顶级父级),因此在容器布置后,子项仍然隐藏.

如果问题太模糊,请不要犹豫,询问更多细节.另外,请尽量不要将我指向SWT文章中的" 理解布局 ".

/**
 * 
 * @author ggrec
 *
 */
public class SSCCE
{

    // ==================== 2. Instance Fields ============================

    private Composite componentContainer;

    private int componentCount = 0;

    // ==================== 3. Static Methods =============================

    public static void main(final String[] args)
    {
        new SSCCE();
    }

    // ==================== 4. Constructors ===============================

    private SSCCE()
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        createContents(shell);

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

    // ==================== 5. Creators ===================================

    private void createContents(final Composite parent)
    {
        parent.setLayout(new GridLayout());
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final Button button = new Button(parent, SWT.PUSH);
        button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        button.setText("Add New Component");

        button.addSelectionListener(new SelectionAdapter()
        {
            @Override public void widgetSelected(final SelectionEvent e)
            {
                addNewComponent();

                componentContainer.layout();
            }
        });

        componentContainer = new Composite(parent, SWT.BORDER);
        componentContainer.setLayout(new GridLayout());
        componentContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    }

    // ==================== 6. Action Methods =============================

    private void addNewComponent()
    {
        final Composite component = new Composite(componentContainer, SWT.BORDER);
        component.setLayout(new FillLayout());
        component.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        final Label label = new Label(component, SWT.NONE);
        label.setText( String.valueOf(componentCount++) );
    }
}
Run Code Online (Sandbox Code Playgroud)

有趣的事实:这个问题是与另一个相关的问题,发布于9分钟前.有人要么是通灵的,要么跟踪我.

gre*_*449 12

Shell调整大小,您需要布局所有内容并重新计算其大小:

shell.layout(true, true);

final Point newSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);  

shell.setSize(newSize);
Run Code Online (Sandbox Code Playgroud)

根据改变的内容,您可以通过调用shell layout()的子项来逃脱Composite.