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

 Swing 中进度指示器的使用主要涉及三个类的使用:
     JProgressBar:用于指示进度的构件
     ProgressMonitor:一个包含进度条的对话框
     ProgressMonitorInputStream:在读取流的时候用于进度监视
下面几个例子依次介绍三个类的使用,首先是JProgressBar,建议先在Eclipse跑一下,在去看源码,我已加上详细的注释,相信很容易看懂。               
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.Timer;
  5. public class ProgressBarTest
  6. {  
  7.    public static void main(String[] args)
  8.    {  
  9.       JFrame frame = new ProgressBarFrame();
  10.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.       frame.setVisible(true);      
  12.    }
  13. }
  14. class ProgressBarFrame extends JFrame
  15. {  
  16.    public ProgressBarFrame()
  17.    {  
  18.       setTitle("ProgressBarTest");
  19.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  20.       // 显示事件的输出
  21.       textArea = new JTextArea();
  22.       JPanel panel = new JPanel();
  23.       startButton = new JButton("Start");
  24.       progressBar = new JProgressBar();
  25.       progressBar.setStringPainted(true);//用来显示 "n%"
  26.       panel.add(startButton);
  27.       panel.add(progressBar);
  28.       checkBox = new JCheckBox("indeterminate");
  29.       checkBox.addActionListener(new 
  30.          ActionListener()
  31.          {  
  32.             public void actionPerformed(ActionEvent event)
  33.             {  //设置成进度不确定模式
  34.                progressBar.setIndeterminate(checkBox.isSelected());
  35.             }
  36.          });
  37.       panel.add(checkBox);
  38.       add(new JScrollPane(textArea), BorderLayout.CENTER);
  39.       add(panel, BorderLayout.SOUTH); 
  40.       //点击start按钮事件
  41.       startButton.addActionListener(new 
  42.          ActionListener()
  43.          {  
  44.             public void actionPerformed(ActionEvent event)
  45.             {  
  46.                progressBar.setMaximum(1000);
  47.                activity = new SimulatedActivity(1000);
  48.                new Thread(activity).start();
  49.                activityMonitor.start();
  50.                startButton.setEnabled(false);
  51.             }
  52.          });
  53.       //没500毫秒更新一次进度条和进度输出
  54.       activityMonitor = new Timer(500new 
  55.          ActionListener()
  56.          {  
  57.             public void actionPerformed(ActionEvent event)
  58.             {  
  59.                int current = activity.getCurrent();
  60.                
  61.                // show progress
  62.                textArea.append(current + "/n");
  63.                progressBar.setStringPainted(!progressBar.isIndeterminate());
  64.                progressBar.setValue(current);
  65.                
  66.                // check if task is completed
  67.                if (current == activity.getTarget())
  68.                {  
  69.                   progressBar.setString("complete!");//当完成是把n%替换成“complete!”
  70.                   activityMonitor.stop();
  71.                   startButton.setEnabled(true);
  72.                }
  73.             }
  74.          });
  75.    }
  76.    private Timer activityMonitor;
  77.    private JButton startButton;
  78.    private JProgressBar progressBar;
  79.    private JCheckBox checkBox;
  80.    private JTextArea textArea;
  81.    private SimulatedActivity activity;
  82.    public static final int DEFAULT_WIDTH = 400;
  83.    public static final int DEFAULT_HEIGHT = 200;
  84. }
  85. /**
  86.    A similated activity runnable.
  87. */
  88. class SimulatedActivity implements Runnable
  89.    public SimulatedActivity(int t)
  90.    {  
  91.       current = 0;
  92.       target = t;
  93.    }
  94.    public int getTarget()
  95.    {  
  96.       return target;
  97.    }
  98.    public int getCurrent()
  99.    {  
  100.       return current;
  101.    }
  102.    public void run()
  103.    {  
  104.       try
  105.       {
  106.          while (current < target)
  107.          {    
  108.             Thread.sleep(100);
  109.             current++;
  110.          }
  111.       }
  112.       catch(InterruptedException e)
  113.       {  
  114.       }
  115.    }
  116.    private volatile int current;//可以同步访问current
  117.    private int target;
  118. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值