java倒数60s实现

 

在JAVA程序中添加一个能倒数的类

 

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
 
class MyTimer extends TimerTask
{
 
 private Timer timer;
 private int time;
 //JLabel soul;
 //JLabel jj;
 MyTimer()
 {
  
 }
 
 MyTimer(Timer timer,int time)
 {
 // this.jj = jj;
 // this.soul = soul; 
  this.time=time;
  this.timer=timer;
 }
 
 public void run()
 {
 System.out.println(this.time);
//  soul.setText(""+time);
  this.time--;
  
  if(this.time<0)
  {
   //JOptionPane.showMessageDialog(null, "你的最终得分"+jj.getText());
      this.timer.cancel();
  }
 }
}
 
 
 
在主函数添加:
    //倒数60s
    Timer t = new Timer();
     int s=60; 
      MyTimer mt=new MyTimer(t,s);
      t.scheduleAtFixedRate(mt,0,1000);
 

 

你可能感兴趣的:(java)