Ver Mensaje Individual
  #3 (permalink)  
Antiguo 16/02/2018, 20:32
sgrajaleshdz
 
Fecha de Ingreso: marzo-2016
Ubicación: World Wide Web
Mensajes: 30
Antigüedad: 8 años
Puntos: 3
Respuesta: Funcion timer que funcione para multiples componentes

He leído, y tratado de implementar el patron singleton de la siguiente manera:

Código Java:
Ver original
  1. public class timer {
  2.     private static timer unicoTimer;
  3.     private Timer t = null;
  4.     private int h, m, s, cs;
  5.    
  6.     JLabel Ltiempo = null;
  7.    
  8.     public static timer getInstancia(){
  9.         if(unicoTimer == null)
  10.             unicoTimer = new timer();
  11.         return unicoTimer;
  12.        
  13.     }
  14.    
  15.     private timer(){}
  16.    
  17.     public void ejecutar(JLabel Label){  
  18.    
  19.     Ltiempo = Label;    
  20.     ActionListener tiempo = new ActionListener(){
  21.        
  22.         @Override
  23.         public void actionPerformed(ActionEvent ae) {
  24.             ++cs;
  25.             if(cs==100){
  26.                 cs = 0;
  27.                 ++s;
  28.             }
  29.             if(s==60)
  30.             {
  31.                 s = 0;
  32.                 ++m;
  33.             }
  34.             if(m==60)
  35.             {
  36.                 m = 0;
  37.                 ++h;
  38.             }
  39.            
  40.             actualizarLabel();
  41.         }
  42.        
  43.     };
  44.    
  45.        
  46.         t = new Timer(10, tiempo);
  47.     }
  48.    
  49.     private void actualizarLabel() {
  50.        
  51.        
  52.        // String tiempo = (h<=9?"0":"")+h+":"+(m<=9?"0":"")+m+":"+(s<=9?"0":"")+s+":"+(cs<=9?"0":"")+cs;
  53.         String tiempo = (h<=9?"0":"")+h+":"+(m<=9?"0":"")+m+":"+(s<=9?"0":"")+s;
  54.        Ltiempo.setText(tiempo);
  55.      
  56.     }
  57.    
  58.     public void start(){
  59.        
  60.         t.start();
  61.     }
  62.    
  63.     public void pausa(){
  64.        
  65.         t.stop();
  66.     }
  67.     public void running(JButton stop){
  68.                
  69.         if(t.isRunning())
  70.         {
  71.             t.stop();
  72.             stop.setEnabled(true);
  73.         }
  74.        
  75.         h=0; m=0; s=0; cs=0;
  76.         actualizarLabel();
  77.     }
  78.  
  79. }

Lo inicio de esta manera:
Código Java:
Ver original
  1. timer.getInstancia().ejecutar(etiquetaTiempo);
  2.         timer.getInstancia().start();
  3.         btnStart.setEnabled(false);
  4.         btnStart.setText("Reanudar");
  5.         btnPause.setEnabled(true);
  6.         btnStop.setEnabled(true);

Se pausa asi:
Código Java:
Ver original
  1. timer.getInstancia().pausa();
  2.         btnStart.setEnabled(true);
  3.         btnPause.setEnabled(false);

y se detiene:
Código Java:
Ver original
  1. timer.getInstancia().running(btnStop);
  2.         btnStart.setText("Iniciar");
  3.         btnPause.setEnabled(false);
  4.         btnStop.setEnabled(false);


Pero sigue sin funcionar al implementarlo aun segundo "Cronometro", no se que pasa pues inicio el primero y funciona, pero al correr el segundo el primero se paraliza y el segundo inicia desde donde se quedo el primero y aumenta la velocidad

Creo no entendí bien el patrón o mi sintaxis es errónea
__________________
Desbloquea esos limites...