Como colocar um relógio em JFrame de um Sistema?

7 respostas
B

Olá.
Tenho um sistema em Swing, to fazendo ele com o Netbeans 6.1 e queria colocar no JFrame Inicial um relógio digital, no VB 2008 tem como fazer isso…
Se alguém souber, me de uma luz… :idea:
Vlw a ajuda

7 Respostas

T
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

public class Clock2 extends JFrame {
	
	JLabel lblClock;
	JPanel panel;
	
	public Clock2() {
		
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		lblClock = new JLabel();
		panel.add(lblClock);
		
		setContentPane(panel);
		
		Timer timer = new Timer(1000, new ClockListener());
		timer.start();
	}
	
	public static void main(String[] args) {
		Clock2 c = new Clock2();
		c.setDefaultCloseOperation(EXIT_ON_CLOSE);
		c.setSize(50, 50);
		c.setVisible(true);
	}
	
	class ClockListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Calendar now = Calendar.getInstance();
			lblClock.setText(String.format("%1$tH:%1$tM:%1$tS", now));
		}
	}

}
B

Bem, esse cód. ai não me ajudo muito, to usando o Netbeans, pra ficar mais claro, coloco quais componentes no JFrame pra poder me basear nesse código?
Vlw a ajuda.

F

Como o tivrfoa mostrou, é um JLabel que é atualizado por um Timer a cada segundo.

B

Ta entendi… agora, mas só 2 perguntas, mexo em alguma propriedade do JLabel?
O Timer eu declaro no mesmo JFrame, mas tenho que atribuir algum método ou la no começo dele posso colocar o cód.?
Vlw o help.

F

Não precisa mexer nas propriedades do JLabel.

Podes declarar bem como o tivrfoa fez.

V

Nesse post:
http://www.guj.com.br/posts/list/52964.java#279083

E nos próximos dois (na segunda página), tem 3 exemplos de como fazer o que você quer.

Netbeans ou não, java é java em qualquer lugar.
Diferente do VB, não faz a menor diferença editar seu código em Netbeans, Eclipse, Notepad, VI…

B
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
  
.import javax.swing.JFrame; 
import javax.swing.JLabel; 
  
public class horario extends JFrame implements ActionListener{ 
     
   private   JLabel horario      =   new   JLabel(); 
   private javax.swing.Timer timer;  
     
   public horario() 
   { 
      super("Horario"); 
      add(horario); 
      disparaRelogio(); 
      setVisible(true); 
      setSize(200, 60); 
      setLocationRelativeTo(null); 
   } 
     
   public static void main(String args[]) 
   {   new horario();   } 
  
  
   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) { 
      Date   hora = new Date(); 
      SimpleDateFormat hora_formato = new SimpleDateFormat("HH:mm:ss"); 
      horario.setText(hora_formato.format(hora)); 
   } 
     
}

Achei esse código aqui mais simples, vo implementar ele la no JLabel.
Vlw a ajuda.

Criado 9 de março de 2009
Ultima resposta 10 de mar. de 2009
Respostas 7
Participantes 4