Relogio

6 respostas
F

Boa noite amigos, preciso por um relogio em uma tela de vendas no sistema que fique atualizando junto com o relogio da maquina. Desde já agradeço.

6 Respostas

BrunoBastosPJ

Como você quer este relógio? Podem ser 3 TextBox para hora, minuto e segunto?

S

Ou então um label, também serve para o efeito. Dá mais pormenores sobre aquilo que tu queres para podermos ser mais objectivos em termos de código.

F

Bem simples! :wink:

public class TimerTest2 extends JPanel {

	JLabel label;

	DateFormat dateFormat;

	public TimerTest2() {
		this.dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		this.add(this.getButton());
		this.go();
	}

	public JLabel getButton() {
		if (this.label == null) {
			this.label = new JLabel(getTime());
		}
		return this.label;
	}

	public void go() {
		ActionListener action = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				label.setText(getTime());
			}
		};
		new Timer(1000, action).start();
	}

	public String getTime() {
		return this.dateFormat.format(Calendar.getInstance().getTime());
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(new TimerTest2());
		frame.setSize(300, 100);
		frame.setVisible(true);
	}
}
F

Olá amigo, veja o que preciso é para uma tela de frente de caixa aonde este relógio tem que ficar rodando (atualizando de segundo em segundo) paresentando a hora atual, podendo ser em um Jlabel ou Jbuton. Forte abraço.

R

Você pode fazer usando JLabel e uma Thread para ficar atualizando. Eu fiz um cronômetro, é um pouco diferente. Você pode pegar o código postado por fabiofalci, modificar o formato que será exibido no label para apenas HH:mm:ss e fazer uma Thread para de segundo em segundo pegar a hora do computador (ou minuto em minuto, sei lá).

Abraços

D

outro exemplo… :wink:

import javax.swing.*;   
    import java.awt.*;   
    import java.awt.event.*;   
    import java.util.*;   
    import javax.swing.JPanel;  
    import javax.swing.JFrame;  
      
    public class Clock implements ActionListener {   
      
      private javax.swing.Timer timer;   
      private Date data;   
      private JLabel label=new JLabel();;   
      private static JFrame frame=new JFrame("Relógio");   
     
      public Clock(){   
         montaTela();  
         disparaRelogio();   
      }   
      public void montaTela(){  
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
            
         label.setFont(new Font("Itálico", Font.ITALIC, 25));   
         JPanel panel = new JPanel();   
         panel.add(label);   
         panel.setLayout(new FlowLayout(FlowLayout.CENTER));   
           
         frame.getContentPane().add(panel);   
     
         frame.setResizable(false);   
         frame.setBounds(250, 200, 150, 80);   
         frame.setLocationRelativeTo(null);  
         frame.setVisible(true);   
       }   
     
      public void disparaRelogio() {   
         if (timer == null) {   
            timer = new javax.swing.Timer(1000, this);   
            timer.setInitialDelay(0);   
            timer.start();   
         } else if (!timer.isRunning()) {   
            timer.restart();   
         }   
      }   
     
      public void actionPerformed(ActionEvent ae) {   
         GregorianCalendar calendario = new GregorianCalendar();   
         int h = calendario.get(GregorianCalendar.HOUR_OF_DAY);   
         int m = calendario.get(GregorianCalendar.MINUTE);   
         int s = calendario.get(GregorianCalendar.SECOND);   
     
         String hora =   
            ((h < 10) ? "0" : "")   
               + h   
               + ":"   
               + ((m < 10) ? "0" : "")   
               + m   
               + ":"   
               + ((s < 10) ? "0" : "")   
               + s;   
     
         label.setText(hora);   
      }   
      public static void main(String args[]) {  
            try{  
            javax.swing.SwingUtilities.invokeLater(new Runnable() {  
               public void run() {   
                new Clock();}  
                        });    
              }  
          catch(Exception e){  
             e.printStackTrace();  
             System.err.println("ERRO interno de execução!");  
             }       
}   
}
Criado 19 de julho de 2008
Ultima resposta 23 de jul. de 2008
Respostas 6
Participantes 6