Saltar al contenido

Usar Temporizador en Java

Hola, hoy posteo aquí cómo funciona un temporizador en Java, para ello este es el código a usar, y en las variables he usado labels de la interfaz gráfica Swing, pero también podemos usar otro tipo de variables.

      try {
            timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
               

                    jLabelTiempo.setText(segundos + «»);
                    segundos = segundos – 1;
                    if (segundos < 0) {//EN CASO QUE EL TEMPORIZADOR SEA 0, ESTO PARARA EL TIEMPO Y SE DESHABILITA COMPONENTES
                        segundos = 10;
                        jLabelTiempo.setText(0 + «»);
                        timer.stop();//TIEMPO PARADO
                        jCheckBox1.setEnabled(false);
                        jCheckBox2.setEnabled(false);
                        jCheckBox3.setEnabled(false);

                    }

                }
            });
        } catch (Exception ex) {
            jLabelTiempo.setText(0 + «»);//EN CASO DE ERROR VUELVE EL TEMPORIZADOR A CERO
        }

timer.start;

Pues nada esto ha sido todo por este tutorial, espero que hayan aprendido algo, y hasta la próxima.

Gracias por visitar mi blog de informática, mi nombre es Tomás y soy formador y desarrollador web. Si quiere usted dejarme alguna sugerencia, ayuda o quiere un servicio de formación estoy escuchando ofertas en tomas.gonzalez@infogonzalez.com, en Facebook a https://www.facebook.com/Infogonzalez estoy deseando escucharle. Su duda o sugerencia NO molesta.

Tomas Gonzalez
error: Content is protected !!
Este sitio web utiliza cookies, si necesitas más información puedes visitar nuestra política de privacidad    Ver
Privacidad
Creative Commons License
Except where otherwise noted, the content on this site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
A %d blogueros les gusta esto: