public class MyClock {
private String current_time;
private java.util.Date date;
private final javax.swing.JLabel label;
public MyClock(javax.swing.JLabel label) {
if (current_time == null) {
java.sql.ResultSet rs = DB.DB.executeQuery("SELECT NOW()");
try {
rs.next();
this.date = rs.getTimestamp(1);
Timer timer = new Timer(1000,new ClockTickAction());
timer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
this.label = label;
}
private class ClockTickAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
date.setTime(date.getTime() + 1000);
label.setText("<html><center>"+new SimpleDateFormat("yyyy.MM.dd").format(date).toString()+
"<br>"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+"</br></center></html>");
}
}
To jest kod ,który realizuje zegar.
Sam zegar jak napisałem wcześniej działa dobrze ale nie wiem czemu stoi w miejscu gdy np. mam włączoną grę na fullscrean.
Może należało by uruchomić ten zegar w oddzielnym wątku ale nie wiem czy o to chodzi.