【发布时间】: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比基本的 SWTorg.eclipse.swt.widgets.Dialog使用起来要简单得多