Java Swing組件實現進度監視功能樣本,javaswing

來源:互聯網
上載者:User

Java Swing組件實現進度監視功能樣本,javaswing

本文執行個體講述了Java Swing組件實現進度監視功能。分享給大家供大家參考,具體如下:

執行個體一:

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ProgressMonitor;import javax.swing.Timer;public class TestProgressMonitor {  Timer timer;  public void init() {    final SimulatedTargetMi target = new SimulatedTargetMi(1000);    // 以啟動一條線程的方式來執行一個耗時的任務    final Thread targetThread = new Thread(target);    targetThread.start();    // 建立進度對話方塊    final ProgressMonitor dialog = new ProgressMonitor(null,        "等待任務完成,任務完成之前請不要關閉視窗,否則將取消當前操作...", "已完成:0.00%", 0,        target.getAmount());    // 建立一個計時器    timer = new Timer(300, new ActionListener() {      public void actionPerformed(ActionEvent e) {        // 以任務的當前完成量設定進度對話方塊的完成比例        dialog.setProgress(target.getCurrent());        dialog.setNote("已完成:" + target.getPercent());        // 如果使用者單擊了進度對話方塊的”取消“按鈕        if (dialog.isCanceled()) {          // 停止計時器          timer.stop();          // 中斷任務的執行線程          targetThread.interrupt();          // 系統退出          System.exit(0);        }      }    });    timer.start();  }  public static void main(String[] args) {    new TestProgressMonitor().init();  }}// 類比一個耗時的任務class SimulatedTargetMi implements Runnable {  // 任務的當前完成量  private volatile int current;  // 總任務量  private int amount;  public SimulatedTargetMi(int amount) {    current = 0;    this.amount = amount;  }  public int getAmount() {    return amount;  }  public int getCurrent() {    return current;  }  // run方法代表不斷完成任務的過程  public void run() {    while (current < amount) {      try {        Thread.sleep(50);      } catch (InterruptedException e) {      }      current++;    }  }  public String getPercent() {    return String.format("%.2f", 100.0 * current / amount) + "%";  }}

運行效果:

執行個體二:

import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax.swing.Timer; public class TestJProgressBar {  JFrame frame = new JFrame("www.jb51.net - 當前進度指示...");  // 建立一條垂直進度條  JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL);  JLabel tipLabel = new JLabel("提示:", JLabel.LEFT);  JLabel contentLabel = new JLabel("任務完成之前請不要關閉視窗,否則將取消當前操作...", JLabel.LEFT);  JLabel statusLabel = new JLabel(" ", JLabel.CENTER);   public void init() {    frame.setLayout(new FlowLayout());    frame.setResizable(false);    tipLabel.setFont(new Font("Serif", Font.PLAIN, 14));    contentLabel.setFont(new Font("Serif", Font.PLAIN, 14));    statusLabel.setFont(new Font("Serif", Font.PLAIN, 14));    JPanel panel = new JPanel();    // fr5.setBorder(new TitledBorder("BoxLayout - Y"));    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));    panel.add(tipLabel);    panel.add(Box.createVerticalStrut(2));    panel.add(contentLabel);    panel.add(Box.createVerticalStrut(7));    panel.add(bar);    // panel.add(Box.createVerticalGlue());    panel.add(Box.createVerticalStrut(2));    panel.add(statusLabel);    frame.add(panel, 0);    final SimulatedTarget target = new SimulatedTarget(1000);    // 以啟動一條線程的方式來執行一個耗時的任務    final Thread thread = new Thread(target);    thread.start();    // 設定在進度條中繪製完成百分比    bar.setStringPainted(true);    // bar.setPreferredSize(new Dimension(100, 18));    // 設定進度條的最大值和最小值,    bar.setMinimum(0);    // 以總任務量作為進度條的最大值    bar.setMaximum(target.getAmount());    final Timer timer = new Timer(300, new ActionListener() {      public void actionPerformed(ActionEvent e) {        // 以任務的當前完成量設定進度條的value        bar.setValue(target.getCurrent());        if (target.getAmount() <= target.getCurrent()) {          statusLabel.setText("處理完成,oh yes!");        }      }    });    timer.start();    frame.setLocationRelativeTo(null);    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.addWindowListener(new WindowAdapter() {      @Override      public void windowClosing(WindowEvent e) {        thread.interrupt();        timer.stop();        // 系統退出        System.exit(0);      }    });    // 該代碼依據放置的組件設定視窗的大小使之正好能容納你放置的所有組件    frame.pack();    frame.setVisible(true);  }   public static void main(String[] args) {    new TestJProgressBar().init();  }} // 類比一個耗時的任務class SimulatedTarget implements Runnable {  // 任務的當前完成量  private volatile int current;  // 總任務量  private int amount;   public SimulatedTarget(int amount) {    current = 0;    this.amount = amount;  }   public int getAmount() {    return amount;  }   public int getCurrent() {    return current;  }   // run方法代表不斷完成任務的過程  public void run() {    while (current < amount) {      try {        Thread.sleep(20);      } catch (InterruptedException e) {      }      current++;    }  }   public String getPercent() {    return String.format("%.1f", 100.0 * current / amount) + "%";  }}

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.