【问题标题】:How to autoset dialog height depending on childrens height?如何根据孩子的身高自动设置对话框高度?
【发布时间】:2019-06-20 18:53:55
【问题描述】:

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

shell.setSize(450, 200);    

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

在 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.pack()
  • 这只是一个 SWT 应用程序还是 Eclipse 插件或 RCP 的一部分? Eclipse JFace org.eclipse.jface.dialogs.Dialog 比基本的 SWT org.eclipse.swt.widgets.Dialog 使用起来要简单得多

标签: java swt composite


【解决方案1】:

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

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

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

pack 应该在设置完 shell 的所有内容后在 layout 之后调用。

您可以通过将 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);

【讨论】:

  • 我试了一下,结果是一个超小的方形对话框,只有一个按钮可见,取消按钮不可见,文本不可见。所以 shell.pack() 不能正常工作,也不知道为什么。我还尝试删除两个 setBounds 调用,结果相同。我附上了这个问题的截图。
  • layout 调用之后立即放置pack。调用 pack 时必须设置所有内容。这可以在这里测试。
  • 是的,这行得通!非常感谢您。但现在我有一个小问题。我需要为对话框设置一个 maxwidth,因为如果文本很长,对话框的宽度很长,高度很小。如何设置标签或外壳的最大宽度?
  • 您可以使用setSizecomputeSize 调用而不是pack 并检查computeSize 返回的值
  • 什么意思?如何处理这些值?
猜你喜欢
  • 2018-10-24
  • 2020-05-29
  • 2019-03-11
  • 2020-05-09
  • 1970-01-01
  • 2018-04-28
  • 2018-06-04
  • 2017-12-17
  • 2019-10-02
相关资源
最近更新 更多