Java Swing 之Timer配合JProgressBar的使用

Timer作为java开发中常用的一个定时工具,配合JProgressBar使用起来还真是方便,只需要调用timer.start()方法就能激活并运行,然后调用stop()方法便能停止,还可以再次通过restart()方法重新使其运行,下面就直接上代码吧:

package UsefulKnowledge;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TimerAndProgressBar implements ActionListener,ChangeListener{

    JFrame f=null;
    JProgressBar progressBar=null;
    JLabel label;
    Timer timer;
    JButton b;

    public TimerAndProgressBar() {
        // TODO Auto-generated constructor stub
        f=new JFrame("ProgressBar and Timer Test");
        Container containerPane=f.getContentPane();
        label=new JLabel("",JLabel.CENTER);
        progressBar=new JProgressBar();
        progressBar.setOrientation(JProgressBar.HORIZONTAL);
        progressBar.setMinimum(0);
        progressBar.setMaximum(100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.addChangeListener(this);
        progressBar.setPreferredSize(new Dimension(200,30));
        JPanel panel=new JPanel();
        b=new JButton("Start");
        b.addActionListener(this);
        panel.add(b);
        timer=new Timer(50,this);
        containerPane.add(panel,BorderLayout.NORTH);
        containerPane.add(progressBar,BorderLayout.CENTER);
        containerPane.add(label,BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
        });
    }

    public static void main(String []args){
        new TimerAndProgressBar();
    }


    @Override
    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub
        int value=progressBar.getValue();
        if(e.getSource()==progressBar){
            label.setText("目前已完成进度:"+Integer.toString(value)+"%");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==b){
            timer.start();
        }
        if(e.getSource()==timer){
            int value=progressBar.getValue();
            if(value<100){
                value++;
                progressBar.setValue(value);
            }else{
                timer.stop();
                progressBar.setValue(0);
            }
        }
    }


}

我们在程序中设置了stateChanged方法来侦听进度条的value的改变,从而达到对其的控制。是不是感觉很方便。

你可能感兴趣的:(Java Swing 之Timer配合JProgressBar的使用)