Created
December 15, 2016 12:20
-
-
Save behitek/25e7c314b000233267c3b5b51870193e to your computer and use it in GitHub Desktop.
Real Time In swing to a label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PlaySchoolClock { | |
public static void main(String[] args) { | |
new PlaySchoolClock(); | |
} | |
public PlaySchoolClock() { | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} catch (ClassNotFoundException ex) { | |
} catch (InstantiationException ex) { | |
} catch (IllegalAccessException ex) { | |
} catch (UnsupportedLookAndFeelException ex) { | |
} | |
JFrame frame = new JFrame("Test"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLayout(new BorderLayout()); | |
frame.add(new ClockPane()); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
} | |
}); | |
} | |
public class ClockPane extends JPanel { | |
private JLabel clock; | |
public ClockPane() { | |
setLayout(new BorderLayout()); | |
clock = new JLabel(); | |
clock.setHorizontalAlignment(JLabel.CENTER); | |
clock.setFont(UIManager.getFont("Label.font").deriveFont(Font.BOLD, 48f)); | |
tickTock(); | |
add(clock); | |
Timer timer = new Timer(500, new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
tickTock(); | |
} | |
}); | |
timer.setRepeats(true); | |
timer.setCoalesce(true); | |
timer.setInitialDelay(0); | |
timer.start(); | |
} | |
public void tickTock() { | |
clock.setText(DateFormat.getDateTimeInstance().format(new Date())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment