对话框是GUI开发中不可缺少的一部分,对话框有系统定义的对话框、消息对话框和自定义对话框等,在这里只简要介绍如下几种类型的对话框:
消息对话框
输入对话框
进度监视对话框
自定义对话框
消息对话框
消息对话框可以显示用户提供帮助,在JFace中,消息对话框由MessageDialog实现。
消息对话框的功能
消息对话框(MessageDialog)是比较常用的对话框,它能够提供便捷的人机交互功能。JFace中提供了MessageDialog来实现消息对话框,它能够提供几种风格的显示。
1. 确认对话框:MessageDialog通过openConfirm方法打开确认对话框,例如“MessageDialog.openConfirm(shell, "Confirm", text.getText());”。
2. 错误对话框:MessageDialog通过openError方法打开错误对话框,例如“MessageDialog.openError(shell, "Error", text.getText());”。
3. 提示信息对话框:MessageDialog通过openInformation方法打开提示信息对话框,例如“MessageDialog.openInformation(shell, "Information", text.getText());”。
4. 提问对话框:MessageDialog通过openQuestion方法打开提问对话框,例如“MessageDialog.openQuestion(shell, "Question", text.getText());”。
5. 警告对话框:MessageDialog通过openWarning方法打开警告对话框,例如“MessageDialog.openWarning(shell, "Warning", text.getText());”。
其中,这几个方法都是静态的方法,且参数都是一样的,第一个参数表示父窗口组件,第二个参数表示标题信息,第三个参数表示显示的信息。
另外,openConfirm返回boolean型的值,表示用户是否选择确认,openQuestion也返回boolean型的值,表示是否正确。
消息对话框实例
创建消息对话框比较简单,在此将通过实例演示每种风格的消息对话框,代码如例程1所示。
例程1 MessageDialogExample.java
/**
*为了节省篇幅,所有的import类已经被注释
*读者可以通过ctrl+shift+o快捷键,自动引入所依赖的类
*如果有问题可发邮件到ganshm@gmail.com
* */
public class MessageDialogExample extends ApplicationWindow {
public MessageDialogExample() {
super(null);
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Send Message");
shell.setSize(500, 400);
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(5, true));
final Text text = new Text(composite, SWT.MULTI | SWT.BORDER
| SWT.V_SCROLL);
GridData data = new GridData(GridData.FILL_BOTH);
data.horizontalSpan = 5;
text.setLayoutData(data);
Button confirm = new Button(composite, SWT.PUSH);
confirm.setText("Confirm");
confirm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button error = new Button(composite, SWT.PUSH);
error.setText("Error");
error.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button information = new Button(composite, SWT.PUSH);
information.setText("Information");
information.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button question = new Button(composite, SWT.PUSH);
question.setText("Question");
question.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button warning = new Button(composite, SWT.PUSH);
warning.setText("Warning");
warning.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Label label = new Label(composite, SWT.NONE);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 5;
label.setLayoutData(data);
final Shell shell = parent.getShell();
confirm.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//显示确认对话框
boolean b = MessageDialog.openConfirm(shell, "Confirm", text
.getText());
//返回是否单击确定按钮(OK)
label.setText("Returned " + Boolean.toString(b));
}
});
error.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//显示错误对话框
MessageDialog.openError(shell, "Error", text.getText());
label.setText("Returned void");
}
});
information.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//显示提示信息对话框
MessageDialog.openInformation(shell, "Information", text
.getText());
label.setText("Returned void");
}
});
question.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//显示提问对话框
boolean b = MessageDialog.openQuestion(shell, "Question", text
.getText());
//返回是否对的按钮(Yes)
label.setText("Returned " + Boolean.toString(b));
}
});
warning.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//显示警告对话框
MessageDialog.openWarning(shell, "Warning", text.getText());
label.setText("Returned void");
}
});
return composite;
}
public static void main(String[] args) {
new MessageDialogExample().run();
}
}
以上代码演示了如何创建每种形式的对话框,程序运行效果如图1所示。
图1 消息对话框
JFace中,消息对话框提供了很多显示的方式,例如显示错误提示信息、显示警告信息和显示提示信息等,用户可以根据需要选择正确的使用方式。
输入对话框
如果要实现用户和应用程序的交互,通常用户输入一些相关的信息,应用程序会根据这些信息进行操作,InputDialog实现了输入对话框的功能。
输入对话框的功能
新建输入对话框(InputDialog)要创建一个InputDialog实例。InputDialog构造函数为:“public InputDialog(Shell parentShell, String dialogTitle,String dialogMessage, String initialValue, IInputValidator validator)”,解释如下。
l parentShell:表示输入框的父窗口。
l dialogTitle:表示输入框的标题。
l dialogMessage:表示输入框的提示信息。
l initialValue:表示初始输入框的值。
l validator:表示输入框的校验器。
校验器是一个IInputValidator类型的对象(也可以为空),校验器中要实现isValid方法,该方法返回一个String类型的值或null,返回值为String类型时为错误的提示信息,如果为null则表示校验正确。下面代码为isValid的实现。
public String isValid(String newText) {
int len = newText.length();
if (len < 5) return "Too short";
if (len > 8) return "Too long";
return null;
}
上面代码表示如果newText的长度大于8或小于5时校验出错,提示错误信息为“Too short”或“Too long”,窗口的确定按钮为禁用。
输入对话框实例
为了更好地理解输入对话框,下面通过实例来介绍。实例中新建一个输入对话框,并加入了校验器,代码如例程2所示。
例程2 InputDialogExample.java
public class InputDialogExample extends ApplicationWindow {
public InputDialogExample() {
super(null);
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Get Input");
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
final Label label = new Label(composite, SWT.NONE);
label.setText("This will display the user input from InputDialog");
Button show = new Button(composite, SWT.PUSH);
show.setText("Get Input");
show.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//新建输入对话框,并添加校验器
InputDialog dlg = new InputDialog(Display.getCurrent()
.getActiveShell(), "", "Enter 5-8 characters", label
.getText(), new LengthValidator());
//打开输入对话框
if (dlg.open() == Window.OK) {