上次给介绍了JProgressBar,这次介绍第二个
ProgressMonitor,他 和JProgressBar没有什么大的区别,ProgressMonitor就是 把JProgressBar放到一个独立的对话框里。构造方法:
更新进度的方法是
还是看一个例子吧:
模拟的一个过程
parentComponent
- 对话框的父组件
message
- 要显示给用户的描述消息,以指示在监视什么操作。
note
- 描述操作状态的简短注释。如果注释最初为 null,则对话框中 将不存在注释行,并且 setNote 无效
min
- 范围的下边界
max
- 范围的上边界
setProgress(int nv)
而JProgressBar更新进度的方法是setValue(int n)
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.ProgressMonitor;
- import javax.swing.Timer;
- public class MyProgressMonitor extends JFrame implements ActionListener{
- public static final int DEFAULT_WIDTH=350;
- public static final int DEFAULT_HEIGHT=450;
- //组件对象
- private JTextArea textArea;
- private JButton start;
- private JScrollPane north;
- private JPanel south;
- //业务对象
- private ActivityTask activityRunnable;//模拟的一个任务
- private Thread activityThread;//模拟任务的线程
- private ProgressMonitor progressDialog;
- private Timer activityMonitor;
- public MyProgressMonitor(){
- textArea = new JTextArea();
- start = new JButton("start");
- north= new JScrollPane(textArea);
- south = new JPanel();
- activityRunnable = new ActivityTask(100);
- activityThread = new Thread(activityRunnable);
- //构建一个进度监视对话框,范围为0,100
- progressDialog = new ProgressMonitor(this,
- "Waiting for Simulated Activity", null,
- 0, activityRunnable.getTarget());
- activityMonitor= new Timer(500,this);//定时器,每各500毫秒执行actionPerformed
- init();
- eventHandle();
- }
- private void init(){
- textArea.setRows(15);
- this.add(north,BorderLayout.NORTH);
- south.add(start);
- this.add(south,BorderLayout.SOUTH);
- }
- private void eventHandle(){
- start.addActionListener(this);
- }
- public void showMe(){
- this.setTitle("ProgressMonitor");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(DEFAULT_HEIGHT, DEFAULT_WIDTH);
- this.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- if(source.equals(this.start)){
- activityThread.start();
- activityMonitor.start();
- start.setEnabled(false);
- }
- if(source.equals(this.activityMonitor)){
- int current = activityRunnable.getCurrent();
- textArea.append(current + "/n");
- progressDialog.setProgress(current);//更新进度
- //当进度到达或者被取消
- if (current == activityRunnable.getTarget() || progressDialog.isCanceled())
- {
- activityMonitor.stop();
- progressDialog.close();
- activityThread.interrupt();
- start.setEnabled(true);
- }
- }
- }
- public static void main(String[] args) {
- new MyProgressMonitor().showMe();
- }
- }
- public class ActivityTask implements Runnable {
- private volatile int current;
- private int target;
- public int getCurrent() {
- return current;
- }
- public int getTarget() {
- return target;
- }
- public ActivityTask(int target) {
- this.target = target;
- }
- public void run() {
- try {
- while (current < target) {
- Thread.sleep(100);
- current++;
- }
- } catch (InterruptedException e) {
- }
- }
- }