在Java中使用進度條

來源:互聯網
上載者:User
文章目錄
  • ProgressMonitorInputStream類繼承層次
  • 構造方法
  • 操作方法
  • 範例程式碼
在讀取大型檔案或者其它大批量資料輸入操作時,希望能夠通過一個進度條顯示當前的進度,現在在Java中非常容易實現,僅僅需要幾行代碼即可。Java的swing包提供了ProgressMonitorInputStream類,該類提供了自動地彈出進度視窗和事件處理機制。使用這個類也非常方便,只需要把任何一個InputStream作為參數,構造一個新的ProgressMonitorInputStream類,其它不需要任何額外的代碼,即可實現進度視窗的自動產生。ProgressMonitorInputStream類可以和其它InputStream一樣使用。
ProgressMonitorInputStream類繼承層次

[pre]java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--javax.swing.ProgressMonitorInputStream[/pre]
構造方法

ProgressMonitorInputStream(Component parentComponent, Object message, InputStream in) 
parentComponent - 觸發被監視操作的組件
message - (如果彈出進度顯示視窗),顯示在進度顯示視窗中的指示資訊
in - 需要監視的輸入資料流
操作方法

除了在InputStream和FilterInputStream中繼承的方法外,還增加了如下方法:

  1.  ProgressMonitor getProgressMonitor() 
  2.    //得到當前對象使用的ProgressMonitor對象。
  3.  int read() 
  4.  int read(byte[] b) 
  5.  int read(byte[] b, int off, int len) 
  6.  void reset() 
  7.  long skip(long n) 
  8.    //上面幾個方法都是覆蓋了FilterInputStream中的方法,因為需要更新進度指示。
  9.  void close() 
  10.    //因為需要關閉進度監視對象和視窗,所以覆蓋了FilterInputStream父類中的close方法。

 
範例程式碼

  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.FileInputStream;
  5. import java.io.InputStream;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.ProgressMonitorInputStream;
  9. public class ProgressMonitorTest {
  10.   public static void main(String[] args) {
  11.     // 建立一個包含“Click me”的視窗
  12.     final JFrame f = new JFrame("ProgressMonitor Sample");
  13.     f.getContentPane().setLayout(new FlowLayout());
  14.     JButton b = new JButton("Click me");
  15.     f.getContentPane().add(b);
  16.     f.pack();
  17.     // 設定按鈕的動作事件
  18.     b.addActionListener(new ActionListener() {
  19.       public void actionPerformed(ActionEvent e) {
  20.         // 這兒使用了新的線程處理按鈕的動作事件,因為我們需要
  21.         //主視窗的線程響應使用者。這樣你可以多次點擊該按鈕,
  22.         //會啟動多個讀取檔案的線程。主視窗也保持響應。
  23.         new Thread() {
  24.           public void run() {
  25.             try {
  26.               // 開啟檔案輸出資料流,把InputStream封裝在ProgressMonitorInputStream中。
  27.               //在目前的目錄中需要放置一個大檔案,建議超過50M
  28.               InputStream in = new FileInputStream("bigfile.dat"); 
  29.               ProgressMonitorInputStream pm = 
  30.                   new ProgressMonitorInputStream(f,"Reading a big file",in);
  31.               // 讀取檔案,如果總耗時超過2秒,將會自動彈出一個進度監看式視窗。
  32.               //   顯示已讀取的百分比。
  33.               int c;
  34.               while((c=pm.read()) != -1) {
  35.                 // 處理代碼
  36.               }
  37.               pm.close(); 
  38.             }
  39.             catch(Exception ex) {
  40.               ex.printStackTrace();
  41.             }
  42.           }
  43.         }.start();
  44.       }});  
  45.   
  46.     // 設定預設的視窗關閉行為,並顯示視窗。
  47.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.     f.setVisible(true);
  49.   }
  50. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.