原创-转载请说明。
ProgressIndicator 可能大家了解的少,我简单写了一个例子,大家参考下吧?
例子都是在本机测试过可以完整运行的。
/**
* @Title: ProgressBarExample.java
* @Description: TODO
* @author zouxs
* @date 2012-9-24
*/
import org.eclipse.jface.dialogs.ProgressIndicator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ProgressBarExample {
private Text text;
private ProgressBar progressBar;
private int max;
private int i;
private int Value;
private int value;
public void ProgessBarExample() throws InterruptedException {
final Display display = Display.getDefault();
final Shell shell = new Shell(SWT.MIN | SWT.CLOSE);
shell.setText("正在下載");
shell.setSize(500, 200);
shell.setLayout(new FillLayout());
final ProgressIndicator progressIndicator = new ProgressIndicator(shell);
progressIndicator.beginTask(100);
Runnable Run = new Runnable() {
public void run() {
for (int i = 0; i < 100; i++) {
try {
// 让线程睡眠0.1秒
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 让UI线程更新滚动条
display.asyncExec(new Runnable() {
public void run() {
progressIndicator.worked(1);
if (progressIndicator.isDisposed())
return;
}
});
}
}
};
new Thread(Run).start();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public static void main(String args[]) throws InterruptedException {
ProgressBarExample pe = new ProgressBarExample();
pe.ProgessBarExample();
}
}