Código de Java - Cronometro

Imágen de perfil
Val: 231
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Cronometrográfica de visualizaciones


Java

Publicado el 8 de Mayo del 2017 por Yacoobs (17 códigos)
28.581 visualizaciones desde el 8 de Mayo del 2017
Bueno esto es coser y cantar como crear un cronometro del tiempo vamos hacer esto de una forma simple utilizando un hilo de ejecución, para ellos echamos manos de la clase Thread les dejo aquí el código que no hay necesidad de mucha explicación mucha suerte y Saludos

Requerimientos

Tener Instalado Java y Netbeans

1
estrellaestrellaestrellaestrellaestrella(8)

Actualizado el 13 de Julio del 2017 (Publicado el 8 de Mayo del 2017)gráfica de visualizaciones de la versión: 1
28.582 visualizaciones desde el 8 de Mayo del 2017
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 *
 * @author Yacoobs Cort. Mart 
 * lunes 8 de mayo de 2017
 * Cronometro con hilo de ejecucion 
 */
public class Cronometro extends javax.swing.JFrame {
 
    /**
     * Creates new form Cronometro
     */
    public Cronometro() {
        initComponents();
    }
 
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jLminuto = new javax.swing.JLabel();
        jLsegundo = new javax.swing.JLabel();
        jLcentesima = new javax.swing.JLabel();
        jB_empezar = new javax.swing.JButton();
        jB_parar = new javax.swing.JButton();
        jLhora = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLminuto.setText("0");
 
        jLsegundo.setText("0");
 
        jLcentesima.setText("0");
 
        jB_empezar.setText("Empezar");
        jB_empezar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_empezarActionPerformed(evt);
            }
        });
 
        jB_parar.setText("Parar");
        jB_parar.setEnabled(false);
        jB_parar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_pararActionPerformed(evt);
            }
        });
 
        jLhora.setText("0");
 
        jLabel1.setText(".");
 
        jLabel2.setText(":");
 
        jLabel3.setText(":");
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jB_empezar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addComponent(jB_parar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLhora, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addGap(10, 10, 10)
                        .addComponent(jLminuto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addGap(10, 10, 10)
                        .addComponent(jLsegundo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLcentesima, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jB_empezar)
                    .addComponent(jB_parar))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLhora)
                    .addComponent(jLminuto)
                    .addComponent(jLsegundo)
                    .addComponent(jLcentesima)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>                        
 
    private void jB_empezarActionPerformed(java.awt.event.ActionEvent evt) {
        jB_parar.setEnabled(true);
        empezar();
        t.start();
    }
 
    private void jB_pararActionPerformed(java.awt.event.ActionEvent evt) {
 
        jB_parar.setEnabled(false);
        jB_empezar.setEnabled(true);
        activar = false;
 
        t.stop();
    }
 
    public void empezar(){
 
        jB_empezar.setEnabled(false);
        activar = true;
        centesima =0;
        segundo = 0;
        minuto =0;
        hora =0;
 
        t = new Thread(new Runnable() {
            @Override
            public void run() {
 
            while (activar){
 
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {}
 
                jLcentesima.setText(""+centesima);
                jLsegundo.setText(""+segundo);
                jLminuto.setText(""+minuto);
                jLhora.setText(""+hora);
 
                centesima++;
                if (centesima>=99){
                    centesima = 0;
                    segundo++;
                }
                if (segundo>=60){
                    segundo = 0;
                    minuto++;
                }
 
                if (minuto>=60){
                    minuto = 0;
                    hora++;
                }
                if (hora>=24){
                   centesima =0;
                   segundo = 0;
                   minuto =0;
                   hora =0;
                }
 
            }
 
 
            }
        });
 
 
 
    }
 
 
    public static void main(String args[]) {
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Cronometro().setVisible(true);
            }
        });
    }
 
    private Thread t;
 
    private boolean activar = false;
    private int segundo,minuto,hora,centesima;
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton jB_empezar;
    private javax.swing.JButton jB_parar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLcentesima;
    private javax.swing.JLabel jLhora;
    private javax.swing.JLabel jLminuto;
    private javax.swing.JLabel jLsegundo;
    // End of variables declaration                   
}



Comentarios sobre la versión: 1 (8)

Imágen de perfil
14 de Julio del 2017
estrellaestrellaestrellaestrellaestrella
muy bueno
Responder
Imágen de perfil
17 de Julio del 2017
estrellaestrellaestrellaestrellaestrella
Se agradece que den las gracias de vez en cuando ... Gracias a ti también.
Responder
Rolando Quintero
3 de Julio del 2018
estrellaestrellaestrellaestrellaestrella
BUENOS DIAS

Esta muy bueno este codigo gracias de antemano
Responder
GMN32
12 de Octubre del 2019
estrellaestrellaestrellaestrellaestrella
muy buen código funciona al 100%
Responder
h151
30 de Octubre del 2017
estrellaestrellaestrellaestrellaestrella
al parecer tiene un atraso de algunos segundos por minuto
Ejemplo:
Cronometro "Celular": -------00:04:10 min
Cronometro "NetBeans": ---00:02:39 min
y cada vez la diferencia sube mas, ¿alguien resolvió este bug?
¿alguien mas tiene este problema?
Responder
Jose Adrian Beniez
20 de Agosto del 2018
estrellaestrellaestrellaestrellaestrella
Excelente para los que empezamos de nuevo
Responder
Ydelina
26 de Marzo del 2019
estrellaestrellaestrellaestrellaestrella
temporizador
Responder
Ydelina
26 de Marzo del 2019
estrellaestrellaestrellaestrellaestrella
Hola, alguien sabe como realizar un temporizador que cierre sesión después de 30 segundos en un Jframe.
Responder

Comentar la versión: 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s3981