Pregunta

I want to set my timer to 0 and show the elapsed time. With this code it starts at 1:00 automatically. But I want to start at 0:00 automatically. Here's my code where it starts at 1:00. How can I set this code to a timer where it starts at 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();

    }*/


}
¿Fue útil?

Solución

You set min and sec = 1. That's your problem but if you change their values to 0 you will start at min = -1. Add a boolean (e.g. init) to your code. So you can check in the if(sec==0){ line if your program just started. Hope you understand what I mean. :-)

...
private boolean init = false;
...
if (sec == 0 && init) {
    sec = 60;
    min--;
} else if (!init) {
    init = true;
}
... 

Btw, I think you should use the javax.swing.Timer.

Otros consejos

Why do you set min = 1 and sec = 1 in line 4? Looks to me like that's the reason why your timer starts at 01:00. Also: how do you call the constructor? If you call it with no argument TypingTime t= new TypingTime(); it will ignore all your setup.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top