Advertisement
rig26

clockpane

May 10th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8.  
  9.  
  10. public class ClockPane extends JPanel {
  11.  
  12. private JLabel clock;
  13.  
  14. public ClockPane(){
  15. setLayout(new BorderLayout());
  16. clock = new JLabel();
  17. clock.setHorizontalAlignment(JLabel.CENTER);
  18. clock.setFont(new Font("Roboto",Font.TRUETYPE_FONT,35));
  19. tickTock();
  20. add(clock);
  21.  
  22. Timer timer = new Timer(500, new ActionListener() {
  23. @Override
  24. public void actionPerformed(ActionEvent e) {
  25. tickTock();
  26. }
  27. });
  28. timer.setRepeats(true);
  29. timer.setCoalesce(true);
  30. timer.setInitialDelay(0);
  31. timer.start();
  32. }
  33.  
  34.  
  35. public void tickTock() {
  36. Date date = new Date();
  37. String strDateFormat = "hh:mm:ss a";
  38. DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
  39. String formattedDate= dateFormat.format(date);
  40. clock.setText(formattedDate);
  41. clock.setForeground(Color.decode("#2B2627"));
  42.  
  43.  
  44. }
  45.  
  46. }
  47. // public void tickTock() {
  48. // clock.setText(DateFormat.getDateTimeInstance().format(new Date()));
  49. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement