Eclipse SWT 对话框(四)
首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。
1. 标准消息对话框
MessageBox可以方便地生成标准消息对话框。
setText()方法设置MessageBox的标题,setMessage()方法设置显示消息。
MessageBox的图标样式,
SWT.ICON_ERROR,显示错误消息。SWT.ICON_WARNING,显示警告消息。SWT.ICON_INFORMATION,显示提示信息。SWT.ICON_WORKING,显示正在进行信息。SWT.ICON_QUESTION,显示问题信息。
MessageBox的按钮样式,默认是SWT.OK。也可以是下列组合,只能选择一种组合或同一组合中几个按钮,否则只会显示确定按钮。
SWT.OK|SWT.CANCEL,确定 | 取消SWT.YES|SWT.NO|SWT.CANCEL,是 | 否 | 取消SWT.ABORT|SWT.RETRY|SWT.IGNORE,中止 | 重试 | 忽略
获取open()方法的返回值,判断用户单击的按钮。
Button defaultButton = new Button(shell, SWT.NONE);
defaultButton.setText("MessageBox");
defaultButton.setBounds(10, 10, 150, 25);
defaultButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 普通消息对话框,默认显示确认按钮
MessageBox mb = new MessageBox(shell, SWT.ICON_SEARCH);
mb.setText("MessageBox");
mb.setMessage("MessageBox open, MessageBox open!");
mb.open();
}
});
Button okOrCancelButton = new Button(shell, SWT.NONE);
okOrCancelButton.setText("Ok|Cancel");
okOrCancelButton.setBounds(10, 45, 150, 25);
okOrCancelButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 显示错误图标,确认和取消按钮
MessageBox mb = new MessageBox(shell,
SWT.ICON_ERROR | SWT.OK | SWT.CANCEL);
mb.setText("Error");
mb.setMessage("A error occur!");
mb.open();
}
});
Button yesOrNoButton = new Button(shell, SWT.NONE);
yesOrNoButton.setText("yes|no");
yesOrNoButton.setBounds(10, 80, 150, 25);
yesOrNoButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 显示提示图标,是、否和取消三个按钮
MessageBox mb = new MessageBox(shell,
SWT.ICON_INFORMATION | SWT.YES | SWT.NO | SWT.CANCEL);
mb.setText("Information");
mb.setMessage("You got a message!");
mb.open();
}
});
Button retryButton = new Button(shell, SWT.NONE);
retryButton.setText("Retry");
retryButton.setBounds(10, 115, 150, 25);
retryButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 显示问题图标,终止、重试和忽略按钮
MessageBox mb = new MessageBox(shell,
SWT.ICON_QUESTION | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
mb.setText("Question");
mb.setMessage("Do you want to try again?");
int result = mb.open();
if (result == SWT.ABORT) {
System.out.println("ABORT Pressed");
} else if (result == SWT.RETRY) {
System.out.println("RETRY Pressed");
} else if (result == SWT.IGNORE) {
System.out.println("IGNORE Pressed");
} else {
System.out.println(result + " Pressed");
}
}
});
显示如下


2. 文本和目录对话框
FileDialog是文本对话框,有三种样式可以选择,SWT.OPEN|SWT.MULTI|SWT.SAVE。样式是SWT.OPEN和SWT.MULTI时,显示为打开。样式是SWT.SAVE时,显示为另存为。
setFilterNames()和setFilterExtensions()为文件对话框设置过滤器,setFilterPath()方法设置默认打开目录。
DirectoryDialog是目录对话框,setFilterPath()方法设置默认打开目录。
Button openButton = new Button(shell, SWT.NONE);
openButton.setText("Open");
openButton.setBounds(10, 10, 150, 25);
openButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 文件对话框,设置文件目录、文件类型和文件格式
FileDialog dialog = new FileDialog(shell);
dialog.setFilterPath("C:\\");
dialog.setFilterNames(new String[]{"Java Files", "Class Files", "All Files"});
dialog.setFilterExtensions(new String[]{"*.java", "*.class", "*.*"});
String result = dialog.open();
System.out.println(result);
}
});
Button multiButton = new Button(shell, SWT.NONE);
multiButton.setText("Multi");
multiButton.setBounds(10, 45, 150, 25);
multiButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 文件对话框,可打开多个文件
FileDialog dialog = new FileDialog(shell, SWT.MULTI);
String result = dialog.open();
if (result != null) {
for (String fileName : dialog.getFileNames()) {
System.out.println(fileName);
}
}
}
});
Button saveButton = new Button(shell, SWT.NONE);
saveButton.setText("Save");
saveButton.setBounds(10, 80, 150, 25);
saveButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 文件保存对话框,设置文件保存目录
FileDialog dialog = new FileDialog(shell, SWT.SAVE);
dialog.setFilterPath("d:\\");
String result = dialog.open();
System.out.println(result);
}
});
Button dirButton = new Button(shell, SWT.NONE);
dirButton.setText("Directory");
dirButton.setBounds(10, 115, 150, 25);
dirButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 文件夹对话框
DirectoryDialog dialog = new DirectoryDialog(shell);
dialog.setFilterPath("d:\\");
String result = dialog.open();
System.out.println(result);
}
});
显示如下


3. 颜色对话框
ColorDialog颜色对话框显示一个调色板并允许用户选择其中一种颜色。
final Button colorButton = new Button(shell, SWT.NONE);
colorButton.setText("Color Dialog");
colorButton.setBounds(10, 10, 150, 25);
colorButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 颜色对话框,返回RGB
ColorDialog dialog = new ColorDialog(shell);
RGB rgb = dialog.open();
if (rgb != null) {
System.out.println(rgb.red + ":" + rgb.green + ":" + rgb.blue);
Color newColor = new Color(display, rgb);
shell.setBackground(newColor);
if (color != null) color.dispose();
color = newColor;
}
}
});
... ...
if (color != null) {
color.dispose();
}
显示如下

4. 字体对话框
FontDialog字体对话框显示当前系统中所有可用字体供用户选择。
final Button fontButton = new Button(shell, SWT.NONE);
fontButton.setText("Font Dialog");
fontButton.setBounds(10, 10, 150, 25);
fontButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 字体对话框,返回FontData
FontDialog dialog = new FontDialog(shell);
FontData fd = dialog.open();
if (fd != null) {
Font newFont = new Font(display, fd);
fontButton.setFont(newFont);
if (font != null) font.dispose();
font = newFont;
}
}
});
... ...
if (font != null) {
font.dispose();
}
}
显示如下

5. 消息对话框
MessageDialog对话框有多种形式。其中openConfirm()和openQuestion()需要通过返回值来判断结果。
Button confirmButton = new Button(shell, SWT.NONE);
confirmButton.setText("Confirm Dialog");
confirmButton.setBounds(10, 10, 150, 25);
confirmButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 打开Confirm对话框,可选择OK或Cancel
boolean result = MessageDialog.openConfirm(shell,
"MessageDialog", "Confirm Dialog");
System.out.println(result);
}
});
Button errorButton = new Button(shell, SWT.NONE);
errorButton.setText("Error Dialog");
errorButton.setBounds(10, 45, 150, 25);
errorButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 打开Error对话框
MessageDialog.openError(shell, "MessageDialog", "Error Dialog");
}
});
Button infoButton = new Button(shell, SWT.NONE);
infoButton.setText("Info Dialog");
infoButton.setBounds(10, 80, 150, 25);
infoButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 打开Information对话框
MessageDialog.openInformation(shell, "MessageDialog", "Info Dialog");
}
});
Button questionButton = new Button(shell, SWT.NONE);
questionButton.setText("Qurstion Dialog");
questionButton.setBounds(10, 115, 150, 25);
questionButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 打开Question对话框,可选择Yes或No
boolean result = MessageDialog.openQuestion(shell,
"MessageDialog", "Qurstion Dialog");
System.out.println(result);
}
});
Button warningButton = new Button(shell, SWT.NONE);
warningButton.setText("Warning Dialog");
warningButton.setBounds(10, 150, 150, 25);
warningButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 打开Warning对话框
MessageDialog.openWarning(shell, "MessageDialog", "Warning Dialog");
}
});
显示如下





MessageDialog属于Jfrace,需要在【Dependencies】里加入org.eclipse.jface和org.eclipse.core.runtime。
6. 输入对话框
InputDialog输入对话框接受输入。
Button inputButton = new Button(shell, SWT.NONE);
inputButton.setText("Input Dialog");
inputButton.setBounds(10, 10, 150, 25);
inputButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
InputDialog dialog = new InputDialog(shell, "输入", "3 + 5 = ?", "", null);
int result = dialog.open();
if (result == InputDialog.OK) {
System.out.println(dialog.getValue());
}
}
});
显示如下
