Olá pessoal!!!
Seguinte: criei um timer em netbeans, apenas como exemplo, pois preciso implementá-lo em outro programa...
Meu exemplo esta assim: um label mostrando o timer, e 3 botoes...
public class tempo4 extends javax.swing.JFrame {
DateFormat dateFormat;
Calendar calendar;
Timer timer;
boolean janela = true;
/** Creates new form tempo4 */
public tempo4() {
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();
initComponents();
}
protected void initialize() {
this.add(this.getLabel());
this.go();
}
public JLabel getLabel() {
if (this.jLabel1 == null) {
this.jLabel1 = new JLabel(getTime());
//this.label.setPreferredSize(new Dimension(100, 22));
}
return this.jLabel1;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
jLabel1.setText(getTime());
}
};
this.timer = new Timer(1000, action);
this.timer.start();
}
public String getTime() {
this.calendar.add(Calendar.SECOND, 1);
return this.dateFormat.format(this.calendar.getTime());
}
//aqui estao os botoes "start", "stop" e "janela", respectivamente...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!timer.isRunning()) {
timer.start();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (timer.isRunning()) {
timer.stop();
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
JOptionPane.showMessageDialog(null, "Você irá para a próxima janela!");
new tempo4().setVisible(true);
}
Só q é o seguinte: reparem q no jButton3 eu fecho minha janela, e ela abre novamente apos a mensagem...
Queria com q, ao abrir novamente a janela, ele ñ reiniciasse o timer, mas q pegasse o tempo q ele parou na ultima exibição do timer, e q ao abrir a janela d novo, comece a contagem a partir da onde ele parou...
Tem como me ajudarem pessoal?!?!
Obrigada desde jah.... ;D
