Zerar contador de tempo

1 resposta
Ermin

Pessoal tenho essa classe Tempo conforme abaixo, implementei o método stop() para para a execução do contador de tempo, alguém poderia me ajudar a fazer um método para zerar o tempo, assim: 00:00:00 ? Vlw e Abraço!

package hora;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
*
* @author ermin
*/
public class Tempo extends JPanel {
    
    Timer t;
    JLabel label;

    DateFormat dateFormat;

    Calendar calendar;

    public Tempo() {
        this.dateFormat = new SimpleDateFormat("HH:mm:ss");
        this.calendar = Calendar.getInstance();
        this.calendar.set(Calendar.MILLISECOND, 0);  
        this.calendar.set(Calendar.SECOND, 0);
        this.calendar.set(Calendar.MINUTE, 0);
        this.calendar.set(Calendar.HOUR_OF_DAY, 0);

        this.initialize();

    }

    protected void initialize() {
        this.setLayout(null);
        this.add(this.getButton());
        this.go();
    }

    public JLabel getButton() {
        if (this.label == null) {
            this.label = new JLabel(getTime());
            this.label.setBounds(10, 10, 100, 22);
        }
        return this.label;
    }

    public void go() {
        ActionListener action = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Tempo.this.label.setText(getTime());
            }
        };
         t = new Timer(1000, action);
        t.start();
    }

    public String getTime() {
        this.calendar.add(Calendar.SECOND, 1);
        return this.dateFormat.format(this.calendar.getTime());
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Tempo());
        frame.setSize(300, 200);

        frame.setVisible(true);
        
    }
    public void stop(){
        t.stop();
    }

   
}

1 Resposta

V

é só setar o valor que vc deseja

igual qndo começou
ex:

set(Calendar.MILLISECOND, 0);
Criado 14 de dezembro de 2010
Ultima resposta 14 de dez. de 2010
Respostas 1
Participantes 2