如何根据孩子的身高自动设置对话高度?

How to autoset dialog height depending on childrens height?

我有一个对话框,可以用更多或更少的文本来调用。现在,我静态设置对话框的高度,设置 200 的高度。

shell.setSize(450, 200);    

问题是,当我向对话框传递一个非常小的文本时,显示的内容有很多空白 space,当我向对话框传递一个长文本时,高度是不够。

在Android中,这个很简单,你可以将WRAP_CONTENT设置为一个布局的高度,然后,它换到他们孩子的高度。

如何在 SWT 中做到这一点?

这是我的对话:

public class ConfirmDialog extends Dialog {
    protected boolean[] result;
    protected Shell shell;
    private String message;
    private String checkboxMessage;
    private Button btnCheckButton;

    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public ConfirmDialog(Shell parent, int style, String message, String checkboxMessage) {
        super(parent, style);
        setText("Confirmación");
        this.message = message;
        this.checkboxMessage = checkboxMessage;
    }

    /**
     * Open the dialog.
     * @return the result
     */
    public boolean[] open() {
        createContents();

        result = new boolean[2];

        Rectangle parentSize = getParent().getBounds();
        Rectangle shellSize = shell.getBounds();
        int x = parentSize.x + (parentSize.width - shellSize.width) / 2;
        int y = (int) (parentSize.y + (parentSize.height - shellSize.height) / 3.5);
        shell.setLocation(new Point(x, y));
        shell.setLayout(new GridLayout(1, false));

        Composite contentComposite = new Composite(shell, SWT.NONE);
        contentComposite.setLayout(new GridLayout(1, false));
        contentComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));

        Label lblText = new Label(contentComposite, SWT.WRAP | SWT.CENTER);
        lblText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
        lblText.setText(message);

        if (checkboxMessage != null) {
            Composite composite_2 = new Composite(shell, SWT.NONE);
            composite_2.setLayout(new GridLayout(1, false));
            composite_2.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));

            btnCheckButton = new Button(composite_2, SWT.CHECK);
            btnCheckButton.setText(checkboxMessage);
            btnCheckButton.setSelection(true);
        }

        Composite buttonsComposite = new Composite(shell, SWT.NONE);
        GridLayout gl_buttonsComposite = new GridLayout(2, false);
        gl_buttonsComposite.horizontalSpacing = 50;
        buttonsComposite.setLayout(gl_buttonsComposite);
        GridData gd_buttonsComposite = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
        gd_buttonsComposite.heightHint = 45;
        buttonsComposite.setLayoutData(gd_buttonsComposite);

        Button acceptButton = new Button(buttonsComposite, SWT.NONE);
        GridData gd_acceptButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_acceptButton.widthHint = 75;
        acceptButton.setLayoutData(gd_acceptButton);
        acceptButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                result[0] = true;
                if (btnCheckButton != null) {
                    result[1] = btnCheckButton.getSelection();
                }
                shell.close();
            }
        });
        acceptButton.setBounds(0, 0, 75, 25);
        acceptButton.setText("Sí");

        Button cancelButton = new Button(buttonsComposite, SWT.NONE);
        GridData gd_cancelButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_cancelButton.widthHint = 75;
        cancelButton.setLayoutData(gd_cancelButton);
        cancelButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.close();
            }
        });
        cancelButton.setBounds(0, 0, 75, 25);
        cancelButton.setText("No");

        shell.setDefaultButton(cancelButton);

        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        return result;
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        shell = new Shell(getParent(), getStyle());
        shell.setSize(450, 200);        
        shell.setText(getText());
    }
}

如果我尝试执行 shell.pack(),结果是一个超小方形对话框,只有一个按钮可见,取消按钮不可见,文本也不可见。所以 shell.pack() 不能正常工作,不知道为什么:

而不是 shell.setSize 只需调用 shell.pack()。这会将大小设置为由 shell 中的布局计算得出的值。 pack 等同于

shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));

此外,您正在将布局(GridLayout 等)与 setBounds 混合使用 - 不要这样做。布局将覆盖边界,坚持只使用布局。

pack应该在layout之后调用,当shell的所有内容都设置好后。

您可以根据计算的大小调整 shell 大小,方法是将 pack 调用替换为类似以下内容:

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

if (size.x > ?????)
  size.x = ????;

shell.setSize(size);

但这只会截断对话框。如果你想滚动,你必须使用类似 ScrolledComposite 的东西。

要使标签环绕文本,您需要为其布局数据指定 widthHint

Label lblText = new Label(contentComposite, SWT.WRAP | SWT.CENTER);

GridData data = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
data.widthHint = 100;  // Maximum width here
lblText.setLayoutData(data);

lblText.setText(message);