📄 progressmonitordialog1.java
字号:
/**
* @作者:陈刚
* @Email:glchengang@yeah.net
* @Blog:http://blog.csdn.net/glchengang
*/
package jface.dialog;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ProgressMonitorDialog1 {
public static void main(String[] args) {
try {
ProgressMonitorDialog1 window = new ProgressMonitorDialog1();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.BORDER);
button.setText(" GO ");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//第一步:创建一个进度条对话框对象
ProgressMonitorDialog pmd = new ProgressMonitorDialog(shell);
//第二步:创建进度条对话框的处理过程对象
IRunnableWithProgress rwp = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("开始执行......", 10);
//10次循环,每次间隔一秒
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (Throwable e2) {
} //间隔一秒
monitor.setTaskName("第" + (i + 1) + "次循环");
monitor.worked(1);//进度条前进一步
}
monitor.done();//进度条前进到底
}
};
try {
//第三步:运行对话框,第二个参数为false表示对话框上的取消按钮不可用
pmd.run(false, false, rwp);
} catch (Exception e3) {
e3.printStackTrace();
}
}
});
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -