Add clock application
This commit is contained in:
44
clock/src/main/java/fr/uha/gasser/clock/Clock.java
Normal file
44
clock/src/main/java/fr/uha/gasser/clock/Clock.java
Normal file
@ -0,0 +1,44 @@
|
||||
package fr.uha.gasser.clock;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class Clock extends JFrame {
|
||||
|
||||
private final JTextField timeField;
|
||||
|
||||
private Clock() {
|
||||
super("Clock example");
|
||||
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
timeField = new JTextField();
|
||||
timeField.setColumns(10);
|
||||
timeField.setEditable(false);
|
||||
|
||||
getContentPane().add(timeField);
|
||||
|
||||
ActionListener timerTask = new ActionListener() {
|
||||
private DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM);
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
timeField.setText(df.format(new Date()));
|
||||
}
|
||||
};
|
||||
|
||||
pack();
|
||||
setVisible(true);
|
||||
|
||||
Timer timer = new Timer(500, timerTask);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// La création de l’interface est confié au DTE
|
||||
EventQueue.invokeLater(Clock::new);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user