📄 dialogsample.java
字号:
package rcpbook.swtjface.sample;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class DialogSample {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(330, 180);
shell.setText("Dialog示例");
shell.setLayout(new RowLayout());
MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO
| SWT.ICON_QUESTION);
messageBox.setMessage("确认删除当前记录?");
messageBox.setText("提示");
messageBox.open();
MessageDialog.openWarning(shell, "提示", "操作错误,请重新导入Excel数据!");
FontDialog fontDialog = new FontDialog(shell);
FontData fontData = fontDialog.open();
if (fontData != null) {
Font font = new Font(shell.getDisplay(), fontData);
// 使用所选择的字体对象
font.dispose();// 使用完毕释放
}
ColorDialog colorDialog = new ColorDialog(shell);
RGB rgb = colorDialog.open();
if (rgb != null) {
Color color = new Color(shell.getDisplay(), rgb);
// 使用所选择的字体对象
color.dispose();// 使用完毕释放
}
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
// 对话框标题
directoryDialog.setText("系统数据备份");
// 对话框提示文字
directoryDialog.setMessage("请选择备份目标文件夹:");
final String detsFolder = directoryDialog.open();
if (detsFolder == null)
MessageDialog.openInformation(shell, "提示", "未选择任何文件!");
FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
fileDialog.setFilterExtensions(new String[] { "*.doc", "*.xls",
"*.ppt", "*.*" });
fileDialog.setFilterPath("%userprofile%/My Documents");
String fileName = fileDialog.open();
if (fileName != null) {
Program.launch(fileName);
}
InputDialog inputDialog = new InputDialog(shell, "数据输入", "请输入邮政编码:",
"000000", new IInputValidator() {
public String isValid(String newText) {
if (newText.length() != 6) {
return "邮政编码长度有误!";
}
return null;
}
});
if (inputDialog.open() == Window.OK)
MessageDialog.openInformation(shell, "提示", "您输入的邮政编码为:"
+ inputDialog.getValue());
else
MessageDialog.openInformation(shell, "提示", "您取消了数据输入!");
Button progress = new Button(shell, SWT.NONE);
progress.setText("显示进度条");
progress.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
new ProgressMonitorDialog(shell).run(true, false,
new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException,
InterruptedException {
monitor.beginTask("开始执行。。。", 100);
for (int i = 0; i < 100; i++) {
monitor.subTask("正在进行第" + i + "步。。。");
monitor.worked(1);
Thread.sleep(100);
}
monitor.done();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -