Pessoal, eu já sei como fazer para pegar uma data e a hora mas gostaria que a hora fosse como um relógio digital ao rodar meu programa a hora a parece e os segundos ficam mudando, teria como fazer isso?? não tenho idéia de como fazer alguém poderia me ajudar??
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjavax.swing.JPanel;importjavax.swing.JFrame;publicclassClockimplementsActionListener{privatejavax.swing.Timertimer;privateDatedata;privateJLabellabel=newJLabel();;privatestaticJFrameframe=newJFrame("Relógio");publicClock(){montaTela();disparaRelogio();}publicvoidmontaTela(){frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);label.setFont(newFont("Itálico",Font.ITALIC,25));JPanelpanel=newJPanel();panel.add(label);panel.setLayout(newFlowLayout(FlowLayout.CENTER));frame.getContentPane().add(panel);frame.setResizable(false);frame.setBounds(250,200,150,80);frame.setLocationRelativeTo(null);frame.setVisible(true);}publicvoiddisparaRelogio(){if(timer==null){timer=newjavax.swing.Timer(1000,this);timer.setInitialDelay(0);timer.start();}elseif(!timer.isRunning()){timer.restart();}}publicvoidactionPerformed(ActionEventae){GregorianCalendarcalendario=newGregorianCalendar();inth=calendario.get(GregorianCalendar.HOUR_OF_DAY);intm=calendario.get(GregorianCalendar.MINUTE);ints=calendario.get(GregorianCalendar.SECOND);Stringhora=((h<10)?"0":"")+h+":"+((m<10)?"0":"")+m+":"+((s<10)?"0":"")+s;label.setText(hora);}publicstaticvoidmain(Stringargs[]){try{javax.swing.SwingUtilities.invokeLater(newRunnable(){publicvoidrun(){newClock();}});}catch(Exceptione){e.printStackTrace();System.err.println("ERRO interno de execução!");}}}
B
b_m_a_
Ai galera, tudo beleza?
Eu fiz um relogio digital em java numa class que extende de JPanel pa poder ser chamada noutro frame, mas nao to conseguindo fazer funcionar
alguem me ajude
public class Clock extends JFrame implements Runnable {
privateJLabelclock;publicClock(){clock=newJLabel();clock.setFont(newFont("Arial",Font.BOLD,22));this.add(clock);setVisible(true);setBounds(250,200,150,80);this.showClock();}publicvoidshowClock(){Strings;Formatformatter;while(true){Datedate=newDate();// Time formate 14.36.33formatter=newSimpleDateFormat("HH:mm:ss a");s=formatter.format(date);clock.setText(s);try{Thread.sleep(1000);}catch(InterruptedExceptione){// TODO Auto-generated catch blocke.printStackTrace();}}}@Overridepublicvoidrun(){this.showClock();}
eu quero que este Panel seja chamado noutra class JFrame e que o relogio fique ali mudando os segundos, funcionando direitinho
quando eu mudo a class do Clock() para extender a um Frame ele funciona certinho.