Swing 中进度指示器的使用(二)

上次给介绍了JProgressBar,这次介绍第二个 ProgressMonitor,他       和JProgressBar没有什么大的区别,ProgressMonitor就是     把JProgressBar放到一个独立的对话框里。构造方法:
public ProgressMonitor( Component  parentComponent, Object  message, String  note, int min, int max);
parentComponent - 对话框的父组件
message - 要显示给用户的描述消息,以指示在监视什么操作。
note - 描述操作状态的简短注释。如果注释最初为 null,则对话框中        将不存在注释行,并且 setNote 无效
min - 范围的下边界
max - 范围的上边界
更新进度的方法是 setProgress(int nv)
JProgressBar更新进度的方法是setValue(int n)

还是看一个例子吧:

  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.JTextArea;
  9. import javax.swing.ProgressMonitor;
  10. import javax.swing.Timer;
  11. public class MyProgressMonitor extends JFrame implements ActionListener{
  12.     public static final int DEFAULT_WIDTH=350;
  13.     public static final int DEFAULT_HEIGHT=450;
  14.     
  15.     //组件对象
  16.     private JTextArea textArea;
  17.     private JButton start;
  18.     private JScrollPane north;
  19.     private JPanel south;
  20.     
  21.     //业务对象
  22.     private ActivityTask activityRunnable;//模拟的一个任务
  23.     private Thread activityThread;//模拟任务的线程
  24.     private ProgressMonitor progressDialog;
  25.     private Timer activityMonitor;
  26.     
  27.     public MyProgressMonitor(){
  28.         textArea = new JTextArea();
  29.         start = new JButton("start");
  30.         north= new JScrollPane(textArea);
  31.         south = new JPanel();
  32.         
  33.         activityRunnable = new ActivityTask(100);
  34.         activityThread = new Thread(activityRunnable);
  35.         
  36.         //构建一个进度监视对话框,范围为0,100
  37.         progressDialog = new ProgressMonitor(this
  38.                 "Waiting for Simulated Activity"null,
  39.                 0, activityRunnable.getTarget());
  40.         activityMonitor= new Timer(500,this);//定时器,每各500毫秒执行actionPerformed
  41.         init();
  42.         eventHandle();
  43.     }
  44.     private void init(){
  45.         textArea.setRows(15);
  46.         this.add(north,BorderLayout.NORTH);
  47.         
  48.         south.add(start);
  49.         this.add(south,BorderLayout.SOUTH);
  50.     }
  51.     private void eventHandle(){
  52.         start.addActionListener(this);
  53.     }
  54.     public void showMe(){
  55.         this.setTitle("ProgressMonitor");
  56.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.         this.setSize(DEFAULT_HEIGHT, DEFAULT_WIDTH);
  58.         this.setVisible(true);
  59.     }
  60.     
  61.     public void actionPerformed(ActionEvent e) {
  62.         Object source = e.getSource();
  63.         if(source.equals(this.start)){
  64.             activityThread.start();
  65.             activityMonitor.start();
  66.             start.setEnabled(false);
  67.         }
  68.         if(source.equals(this.activityMonitor)){
  69.             int current = activityRunnable.getCurrent();
  70.             textArea.append(current + "/n");
  71.             progressDialog.setProgress(current);//更新进度
  72.             
  73.             //当进度到达或者被取消
  74.             if (current == activityRunnable.getTarget() || progressDialog.isCanceled())
  75.             {  
  76.                activityMonitor.stop();
  77.                progressDialog.close();
  78.                activityThread.interrupt();
  79.                start.setEnabled(true);
  80.             }
  81.         }
  82.     }
  83.     
  84.     public static void main(String[] args) {
  85.         new MyProgressMonitor().showMe();
  86.     }
  87. }
模拟的一个过程
  1. public class ActivityTask implements Runnable {
  2.     private volatile int current;
  3.     private int target;
  4.     public int getCurrent() {
  5.         return current;
  6.     }
  7.     public int getTarget() {
  8.         return target;
  9.     }
  10.     public ActivityTask(int target) {
  11.         this.target = target;
  12.     }
  13.     public void run() {
  14.         try {
  15.             while (current < target) {
  16.                 Thread.sleep(100);
  17.                 current++;
  18.             }
  19.         } catch (InterruptedException e) {
  20.         }
  21.     }
  22. }










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值