Relógio duvida

1 resposta
AnDrOiDe

Bom dia galera,estou com um codigo mas to com duvida de como chamar ele no meu frame , criei uma label para mostrar o relogio em funcionamento:

segue o cod

public class Relogio extends JLabel {   
  
    private final DateFormat format;   
       
    public Relogio() {           
        format = new SimpleDateFormat("HH:mm");   
           
        Thread thread = new Thread(new Runnable() {   
            @Override  
            public void run() {   
                while (true) {   
                    doTime();   
                    try {   
                        Thread.sleep(59000L);   
                    } catch (InterruptedException ex) {   
                        Thread.currentThread().interrupt();   
                        break;   
                    }   
                }   
            }   
        }, "Relogio");   
        thread.setDaemon(true);   
        thread.setPriority(Thread.MIN_PRIORITY + 1);   
        thread.start();   
    }   
       
    private void doTime() {   
        super.setText(format.format(new Date()));   
    }   
       
    @Override  
    public void setText(String text) {   
    }   
}

1 Resposta

fernandopaiva

opa, veja o exemplo

public class Relogio{
   private JLabel relogio = new JLabel();
   private SimpleDateFormat horaFormatada = new SimpleDateFormat("HH:mm:ss"); 

    public Relogio(){
        iniciaRelogio();
    }

public void iniciaRelogio() {
        ActionListener action = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 relogio.setText(horaFormatada.format(new Date()));
             }
        };
        javax.swing.Timer timer = new javax.swing.Timer(1000, action);
        timer.setInitialDelay(0);
        timer.setRepeats(true);
        timer.start();            
    }


}

t+ e boa sorte

Criado 18 de outubro de 2011
Ultima resposta 18 de out. de 2011
Respostas 1
Participantes 2