Studyon Minte9.com
Java SE




Timer (clock)




package testexample; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import java.util.TimeZone; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; class Testexample extends JFrame { private JTextField textField; public Testexample() { textField = new JTextField(5); textField.setEditable(false); textField.setFont(new Font("sansserif", Font.PLAIN, 48)); textField.setBorder(new EmptyBorder(0,0,0,0)); JPanel panel = new JPanel(); panel.add(textField); add(panel); // --- Look Here --- // javax.swing.Timer timer = new javax.swing.Timer(1000, new ClockListener()); // one second timer.start(); } public static void main(String[] args) { JFrame test = new Testexample(); test.setBounds(200, 200, 300, 100); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); } class ClockListener implements ActionListener { // --- Look Here --- // @Override public void actionPerformed(ActionEvent e) { Calendar now = Calendar.getInstance(); now.setTimeZone(TimeZone.getTimeZone("Europe/Bucharest")); int h = now.get(Calendar.HOUR_OF_DAY); int m = now.get(Calendar.MINUTE); int s = now.get(Calendar.SECOND); textField.setText(h + ":" + m + ":" + s); } } }
 


http://www.leepoint.net/notes-java/examples/animation/41TextClock/25textclock.html