SWT Shell会根据孩子的大小调整大小 [英] SWT Shell resize depending on children

查看:120
本文介绍了SWT Shell会根据孩子的大小调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理这个 Composite 画布,其中可以添加和删除其他 Composite

I'm working on this Composite canvas on which other Composites may be added and removed.

我对整个布局概念如何处于迷雾中的理解。

My understanding of how the whole laying-out concept is still in the fog.

当孩子们添加到容器中,假设容器有一个 GridData 填充父级,父级是否也应该知道子级调整大小?由于贝壳(顶级父母),儿童在容器布置后仍然隐藏。

When children are added to the container, given the fact that the container has a GridData which fills in the parent, shouldn't the parent also know that the child resized? Children remain hidden after their container has been laid out, because of the shell (top parent).

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

If the question is too vague, do not hesitate to ask for more details. Also, try your best not pointing me to the Understanding layouts in SWT article.

/**
 * 
 * @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++) );
    }
}

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

Fun fact: This question is veeeery related to this other one, that was posted 9 minutes ago. Someone is either psychic or stalking me.

推荐答案

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

To get the Shell to resize you need to layout everything and recalculate its size:

shell.layout(true, true);

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

shell.setSize(newSize);

您可以通过调用 layout()关于shell的子 Composite ,具体取决于更改的内容。

You may be able to get away with calling layout() on a child Composite of the shell depending on what has changed.

这篇关于SWT Shell会根据孩子的大小调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆