Hacer un programa que muestre un cronometro, indicando las horas, minutos y segundos. dame el codigo como si lo ubiera echo un novato
Aquí te dejo un ejemplo de cómo crear un cronómetro en Java que muestra las horas, minutos y segundos. Este programa utiliza la clase `Thread` para crear un hilo que actualiza el cronómetro cada segundo. ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Cronometro extends JFrame { private JLabel horaLabel; private JLabel minutoLabel; private JLabel segundoLabel; private int segundos = 0; private int minutos = 0; private int horas = 0; public Cronometro() { setLayout(new FlowLayout()); horaLabel = new JLabel("00"); horaLabel.setFont(new Font("Arial", Font.BOLD, 24)); add(horaLabel); minutoLabel = new JLabel("00"); minutoLabel.setFont(new Font("Arial", Font.BOLD, 24)); add(minutoLabel); segundoLabel = new JLabel("00"); segundoLabel.setFont(new Font("Arial", Font.BOLD, 24)); add(segundoLabel); JButton startButton = new JButton("Iniciar"); startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { start(); } }); add(startButton); JButton stopButton = new JButton("Detener"); stopButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { stop(); } }); add(stopButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { reset(); } }); add(resetButton); setSize(300, 100); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private void start() { Thread thread = new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(1000); segundos++; if (segundos == 60) { minutos++; segundos = 0; } if (minutos == 60) { horas++; minutos = 0; } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { horaLabel.setText(String.format("%02d", horas)); minutoLabel.setText(String.format("%02d", minutos)); segundoLabel.setText(String.format("%02d", segundos)); } }); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }); thread.start(); } private void stop() { Thread thread = Thread.currentThread(); thread.interrupt(); } private void reset() { segundos = 0; minutos = 0; horas = 0; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { horaLabel.setText("00"); minutoLabel.setText("00"); segundoLabel.setText("00"); } }); } public static void main(String[] args) { new Cronometro(); } } ``` Este programa crea una ventana con un cronómetro que muestra las horas, minutos y segundos. El cronómetro se puede iniciar, detener y resetear mediante botones. El cronómetro actualiza automáticamente cada segundo. Recuerda que este es un ejemplo básico y puedes personalizarlo según tus necesidades.