SET/JFace ProgressIndicator的使用以及来回滚动进度条实际使用示例

       关于进度条的意义,不多说了,之前了解过SWT的ProgressBar,网上教程很多,自己测试也没问题,但是当我尝试将这个东西应用到实际项目中时,才发现真的好难,因为实际的项目中,长时间处理的东西都在主线程中,一旦执行起这些任务,SWT的这个进度条就动不了了,特别麻烦,后来反编译了TC的登录界面,发现用了一个叫ProgressIndicator的东西,一看源码,原来是JFace的东西,稍微研究了下,确实比较好用,原来以为JFace把线程那些都封装了,只要调用就可以,兴致勃勃地用到项目里,结果一执行,又傻眼了,还是跟SWT的那个一样,进度条不动!后来还是用多线程实现了功能,在新开辟的线程里,我可以处理自己的业务,然后再将相应结果显示在界面上,例子里是计算圆周率的算法,比较耗时间。感觉比那些教程里直接线程休眠比较容易理解。

  1 import org.eclipse.jface.dialogs.ProgressIndicator;
  2 import org.eclipse.swt.SWT;
  3 import org.eclipse.swt.events.SelectionAdapter;
  4 import org.eclipse.swt.events.SelectionEvent;
  5 import org.eclipse.swt.widgets.Button;
  6 import org.eclipse.swt.widgets.Display;
  7 import org.eclipse.swt.widgets.Label;
  8 import org.eclipse.swt.widgets.Shell;
  9 
 10 public class ProgressIndicatorTest {
 11 
 12     protected Shell shell;
 13     private ProgressIndicator progressIndicator;
 14     private Label lblNewLabel;
 15     private double result;
 16 
 17     /**
 18      * Launch the application.
 19      * @param args
 20      */
 21     public static void main(String[] args) {
 22         try {
 23             ProgressIndicatorTest window = new ProgressIndicatorTest();
 24             window.open();
 25         } catch (Exception e) {
 26             e.printStackTrace();
 27         }
 28     }
 29 
 30     /**
 31      * Open the window.
 32      */
 33     public void open() {
 34         Display display = Display.getDefault();
 35         createContents();
 36         shell.open();
 37         shell.layout();
 38         while (!shell.isDisposed()) {
 39             if (!display.readAndDispatch()) {
 40                 display.sleep();
 41             }
 42         }
 43     }
 44 
 45     /**
 46      * Create contents of the window.
 47      */
 48     protected void createContents() {
 49         shell = new Shell(SWT.CLOSE);
 50         shell.setSize(350, 80);
 51         shell.setText("圆周率计算");
 52         shell.setLayout(null);
 53         
 54         Button btnNewButton = new Button(shell, SWT.NONE);
 55         btnNewButton.setBounds(128, 28, 81, 20);
 56         btnNewButton.setText("计算");
 57         
 58         progressIndicator = new ProgressIndicator(shell,SWT.BORDER);
 59         progressIndicator.setBounds(0, 15, 350, 10);
 60         
 61         lblNewLabel = new Label(shell, SWT.NONE);
 62         lblNewLabel.setBounds(0, 0, 350, 16);
 63         lblNewLabel.setAlignment(SWT.CENTER);
 64         lblNewLabel.setText("");
 65 
 66         btnNewButton.addSelectionListener(new SelectionAdapter() {
 67             @Override
 68             public void widgetSelected(SelectionEvent e) {
 69                 btnNewButton.setEnabled(false);
 70                 progressIndicator.beginAnimatedTask();
 71                 lblNewLabel.setText("正在计算,请稍后...");
 72                 new Thread(new Runnable() {
 73                     
 74                     @Override
 75                     public void run() {
 76                         long start = System.currentTimeMillis();
 77                         result = start(10000000000L);
 78                         long end = System.currentTimeMillis();
 79                         long time = (end-start)/100;
 80                         final float t = time/10.0f;
 81                         Display.getDefault().syncExec(new Runnable() {
 82                             
 83                             @Override
 84                             public void run() {
 85                                 lblNewLabel.setText("计算完成,耗时:"+t+"秒  计算结果为:"+result);
 86                                 progressIndicator.done();
 87                                 btnNewButton.setEnabled(true);
 88                             }
 89                         });
 90                     }
 91                 }).start();
 92             }
 93             private double start(long n){
 94                 int i = 1;
 95                 double x = 0d;
 96                 while(i<=n){
 97                     if(i%2==0){
 98                         x = x-1.00/(2*i-1);
 99                     }else{
100                         x = x+1.00/(2*i-1);
101                     }
102                     i++;
103                 }
104                 return x*4;
105             }
106         });
107     }
108 }

 

      

posted @ 2018-02-05 17:35  墨梅一点清  阅读(421)  评论(0编辑  收藏  举报