一个计时器按钮

一个简单的计时器按钮,相当简单,但却很有用,放在这儿分享下。

功能:点击开始倒计时,再次点击暂停/开始。

 

 1 import java.awt.Font;

 2 import java.awt.event.ActionEvent;

 3 import java.awt.event.ActionListener;

 4 import java.text.SimpleDateFormat;

 5 import java.util.Date;

 6 

 7 import javax.swing.ImageIcon;

 8 import javax.swing.JButton;

 9 import javax.swing.JLabel;

10 import javax.swing.Timer;

11 

12 public class TimerButton extends JButton implements ActionListener{

13     Date now = null;

14     Timer timer = null;

15     boolean flag=false;

16     boolean flag2=false;

17     Font font=null;

18     public TimerButton(String btnName,int time,ImageIcon timerIcon) {

19         // TODO Auto-generated constructor stub

20         //timerIcon=new ImageIcon("images/timer.gif");

21         font=new Font("幼圆",Font.BOLD,70);

22         this.setIcon(timerIcon);

23         this.setText("<html><h1>"+btnName+"</h1></html>");

24         this.addActionListener(this);

25         now=new Date();

26         //设置倒计时时间.

27         now.setHours(0);

28         now.setMinutes(time);

29         now.setSeconds(0);

30         timer = new Timer(1000, new ActionListener() {

31             public void actionPerformed(ActionEvent e) {

32                 now = new Date(now.getTime() - 1000);

33                 //SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

34                 SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");

35                 TimerButton.this.setFont(TimerButton.this.font);

36                 TimerButton.this.setText(formatter.format(now));

37             }

38         });

39     }

40     public TimerButton(String btnName,int time) {

41         // TODO Auto-generated constructor stub

42         //timerIcon=new ImageIcon("images/timer.gif");

43         font=new Font("幼圆",Font.BOLD,70);

44         this.setText("<html><h1>"+btnName+"</h1></html>");

45         this.addActionListener(this);

46         now=new Date();

47         //设置倒计时时间.

48         now.setHours(0);

49         now.setMinutes(time);

50         now.setSeconds(0);

51         timer = new Timer(1000, new ActionListener() {

52             public void actionPerformed(ActionEvent e) {

53                 now = new Date(now.getTime() - 1000);

54                 //SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

55                 SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");

56                 TimerButton.this.setFont(TimerButton.this.font);

57                 TimerButton.this.setText(formatter.format(now));

58             }

59         });

60     }

61     @Override

62     public void actionPerformed(ActionEvent arg0) {

63         // TODO Auto-generated method stub

64         if(arg0.getSource().equals(TimerButton.this)){

65             if(flag==false){

66                 this.setIcon(null);

67                 this.setFont(new Font("幼圆",Font.BOLD,60));

68                 this.setText("计时开始");

69                 timer.start();

70                 flag=true;

71             }

72             else{

73                 if(flag2==false){

74                     try {

75                         timer.stop();

76                     } catch (Exception e) {

77                         // TODO Auto-generated catch block

78                         e.printStackTrace();

79                     }

80                     flag2=true;

81                 }else{

82                     timer.start();

83                     flag2=false;

84                 }

85             }

86             

87         }

88     }

89 }

你可能感兴趣的:(计时器)