我想将我的计时器设置为 0 并显示经过的时间。使用此代码,它会自动从 1:00 开始。但我想自动从 0:00 开始。这是我的代码,它从 1:00 开始。如何将此代码设置为从 0:00 开始的计时器?
class TimeTyping extends JPanel implements Runnable{
private JLabel timelabel;
private int sec=1,min=1,testtime=0;
private String str;
private boolean timefree;
protected Thread t1 ;
TimeTyping(int time){
super();
setLayout(new FlowLayout());
setVisible(true);
setSize(300,300);
if(time<=5){
str=""+time+":00";
timelabel=new JLabel(str);
timefree=false;
}
else{
timelabel=new JLabel("Free Time Test");
timefree=true;
}
add(timelabel);
t1 = new Thread(this);
/*t1.start();
t1.suspend();*/
}
public void run(){
try{
if(timefree){
testtime++;
run();
System.out.println("free");
}
else{
//taking min and sec form label
synchronized(this) {
if(sec==0){
sec=60;
min--;
}
sec++;
testtime++;
if(sec>9){
timelabel.setText(min+":"+sec);
}
else{
timelabel.setText(min+":0"+sec);
}
}
Thread.sleep(1000);
//if test is not over
if(min>0 || sec>0){
run();
}
}
}
catch(Exception e){}
}
//return total test time taken in sec
public int getTestTime(){
return testtime;
}
//return minutes of test time left
public int getMin(){
return min;
}
//return sec of test time left
public int getSec(){
return sec;
}
/*public static void main(String args[]){
TypingTime t= new TypingTime();
}*/
}