Advertisement
Yudhizth

ClockWithTimer

May 26th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package learning;
  7.  
  8. /**
  9.  *
  10.  * @author lantisse
  11.  */
  12.  
  13. import java.awt.Font;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.Timer;
  21.  
  22.  
  23. public class DigitalClock extends JFrame implements ActionListener {
  24. JLabel timeLabel = new JLabel();
  25. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  26. Timer timer;
  27.  
  28. public DigitalClock() {
  29.  
  30. super("Tes");
  31. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
  33. timeLabel.setFont(new Font("Proxima Nova Alt", Font.PLAIN, 24));
  34. timer = new Timer(500, this);
  35. timer.setRepeats(true);
  36. timer.start();
  37. this.add(timeLabel);
  38. this.pack();
  39. this.setVisible(true);
  40.         }
  41.  
  42.     @Override
  43.     public void actionPerformed(ActionEvent e) {
  44.         if (e.getSource().equals(timer)) {
  45. // Then set a new time.
  46. timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
  47.         }
  48.  
  49.     }
  50.    
  51.     public static void main(String[] args) {
  52. // Create the digital clock.
  53. new DigitalClock();
  54.  
  55.         }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement