程式扎記: [ Java 代碼範本 ] UI - Progress bar sample code

標籤

2012年10月29日 星期一

[ Java 代碼範本 ] UI - Progress bar sample code

來源自 這裡 
Preface: 
在使用 Java Swing 開發 UI 程式時, 有時候需要進行耗時工作時, 可以使用 Progress bar 提示使用者目前工作進度或是告訴使用者目前程式的工作狀態. 

Demo Code: 
下面代碼執行會開啟一個 UI, 並在上面顯示 Progress bar. 在背景使用 SwingWorker (類別 UpdateWorker) 動態的更新 Progress bar 的進度條: 
 

當進度條上的數字達到 100, 則 SwingWorker 方法 done() 會被執行並關閉 UI 與結束程式. 

Implementation: 
  1. package swing.demo;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Container;  
  5. import java.util.Random;  
  6.   
  7. import javax.swing.BorderFactory;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.JProgressBar;  
  10. import javax.swing.SwingWorker;  
  11. import javax.swing.border.Border;  
  12.   
  13. public class ProgressSample {  
  14.     public static void main(String args[]) {  
  15.         JFrame f = new JFrame("JProgressBar Sample");  
  16.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  17.         Container content = f.getContentPane();  
  18.         JProgressBar progressBar = new JProgressBar();  
  19.         progressBar.setValue(0);  
  20.         progressBar.setStringPainted(true);  
  21.         Border border = BorderFactory.createTitledBorder("Reading...");  
  22.         progressBar.setBorder(border);  
  23.         content.add(progressBar, BorderLayout.NORTH);  
  24.         f.setSize(300100);  
  25.         f.setVisible(true);  
  26.         UpdateWorker updateWorker = new UpdateWorker(f, progressBar);  
  27.         updateWorker.execute();  
  28.     }  
  29.       
  30.     public static class UpdateWorker extends SwingWorker   
  31.     {  
  32.         JProgressBar bar = null;  
  33.         JFrame f=null;  
  34.         public UpdateWorker(JFrame f, JProgressBar bar)  
  35.         {  
  36.             this.bar = bar;  
  37.             this.f = f;  
  38.         }         
  39.           
  40.         @Override  
  41.         protected String doInBackground() throws Exception {  
  42.             Random rdm = new Random();  
  43.             int pv = 0;  
  44.             while(pv<100)  
  45.             {  
  46.                 Thread.sleep(rdm.nextInt(500)+500);  
  47.                 pv+=rdm.nextInt(5);  
  48.                 bar.setValue(pv);  
  49.             }  
  50.             return null;  
  51.         }   
  52.           
  53.         @Override    
  54.         protected void done()   
  55.         {   
  56.             f.setVisible(false);      
  57.             f.dispose();  
  58.         }  
  59.     }  
  60. }  
Supplement: 
The Java Tutorial > How to Use Progress Bars 
Sometimes a task running within a program might take a while to complete. A user-friendly program provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done. One way of indicating work, and perhaps the amount of progress, is to use an animated image...

[ Java 代碼範本 ] SwingWorker Example

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!