Ciao a tutti… eccomi intanto con un altro problema
Ho creato un timer che mi serve a scandire il tempo…
Dopo di che ho creato un gestore di bottoni (bottoni che ho inserito in un jpanel)…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; } } } }
i bottoni sono: avvio, pausa, stop:
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.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); } } }
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?


Rispondi quotando
