Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2014
    Messaggi
    168

    Timer: sospensione e riavvio

    Ciao a tutti… eccomi intanto con un altro problema
    Ho creato un timer che mi serve a scandire il tempo…


    codice:
    JTextField tempo;
    Thread t = null;
    Watch watch = new Watch();
     
           public class Watch implements Runnable {
                  int hours = 0;
                  int minutes = 0;
                  int seconds = 0;
    boolean runTimer = true;
                
                 public void isRunning(boolean value) {
                        runTimer = value;
                 }
                
                 public void run() {
                     Timer timer = new Timer(1000, new TimerListenerClass());
                     timer.start();
                    
                     while (runTimer) {
                        tempo.setText(hours + ":" + minutes + ":" + seconds);
                     }
                 }
                
                 class TimerListenerClass implements ActionListener {
                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                               seconds += 1;
                               if (seconds > 59) {
                                      seconds = 0;
                                      minutes += 1;
                               }
                               if (minutes > 59) {
                                      minutes = 0;
                                      hours += 1;
                               }
                        }
                 }
           }
    Dopo di che ho creato un gestore di bottoni (bottoni che ho inserito in un jpanel)…
    i bottoni sono: avvio, pausa, stop:

    codice:
           private class GestoreBottoni implements ActionListener {
                 public void actionPerformed(ActionEvent e) {
                        if (e.getSource() == avvio) {
                               if (t == null) {
                                       t = new Thread (watch);
                                       t.start(); 
                                       avvio.setEnabled(false);
                                       pausa.setEnabled(true);
                                       stop.setEnabled(true);
                               } else {
                                      watch.isRunning(true);
                               }      
                        } else if (e.getSource() == pausa) {
                               watch.isRunning(false);
                               avvio.setEnabled(true);
                        } else if (e.getSource() == stop) {
                               watch.isRunning(false);           
                               pausa.setEnabled(false);
                        }
                 }
           }
    Quando premo il bottone ‘avvio’ deve partire il tempo e stampare il tempo che passa sul jtextField ‘tempo’. Successivamente posso premere il bottone ‘stop’ per bloccare il timer, oppure ‘pausa’ per sospenderlo e poterlo riavviare successivamente da dove l’avevo interrotto.
    Il mio problema è che non riesco a mettere in pausa il timer e riavviarlo… ho provato ad usare t.suspend() e t.resume() ma mi vengono barrate e mi esce scritto che sono obsolete… come potrei fare?

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2014
    Messaggi
    168
    Nessun aiuto?

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2014
    Messaggi
    168

  4. #4
    Utente di HTML.it
    Registrato dal
    Jul 2014
    Messaggi
    168
    Non c'è modo per sospendere e riavviare il timer?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.