我有一个对话框,可以使用更多或更少的文本来调用。现在,我将静态设置对话框的高度,将高度设置为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()无法正常工作,也不知道为什么:
答案 0 :(得分:2)
致电shell.setSize
而不是shell.pack()
。这会将大小设置为由外壳中的布局计算的值。 pack
等同于
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
另外,您正在将布局(GridLayout
等与setBounds
混合-请勿这样做。布局将覆盖边界,仅使用布局即可。
pack
之后调用 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);