
Mostrar la hora a partir de una hora dada
Java
Publicado el 28 de Octubre del 2019 por Administrador (718 códigos)
1.746 visualizaciones desde el 28 de Octubre del 2019
Código que dada una hora dada, va incrementando dicha hora.


import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FrmContador extends javax.swing.JFrame implements Observer{
public FrmContador() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
lblCronometro = new javax.swing.JLabel();
btnIniciar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Reloj digital");
lblCronometro.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
btnIniciar.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
btnIniciar.setText("Iniciar");
btnIniciar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnIniciarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(lblCronometro, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnIniciar, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblCronometro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnIniciar, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}
private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
this.btnIniciar.setEnabled(false);
RelojDigital r = new RelojDigital(23, 59, 50);
r.addObserver(this);
Thread t = new Thread(r);
t.start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrmContador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrmContador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmContador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmContador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrmContador().setVisible(true);
}
});
}
private javax.swing.JButton btnIniciar;
private javax.swing.JLabel lblCronometro;
@Override
public void update(Observable o, Object arg) {
lblCronometro.setText((String) arg);
}
}
class RelojDigital extends Observable implements Runnable {
private int horas, minutos, segundos;
public RelojDigital(int horas, int minutos, int segundos) {
this.horas = horas;
this.minutos = minutos;
this.segundos = segundos;
}
@Override
public void run() {
String tiempo;
try {
while (true) {
tiempo = "";
if (horas < 10) {
tiempo += "0" + horas;
} else {
tiempo += horas;
}
tiempo += ":";
if (minutos < 10) {
tiempo += "0" + minutos;
} else {
tiempo += minutos;
}
tiempo += ":";
if (segundos < 10) {
tiempo += "0" + segundos;
} else {
tiempo += segundos;
}
this.setChanged();
this.notifyObservers(tiempo);
this.clearChanged();
Thread.sleep(1000);
segundos++;
if (segundos == 60) {
minutos++;
segundos = 0;
if (minutos == 60) {
minutos = 0;
horas++;
if (horas == 24) {
horas = 0;
}
}
}
}
} catch (InterruptedException ex) {
Logger.getLogger(RelojDigital.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Comentarios sobre la versión: 20190515 (0)
No hay comentarios