I want to make a java.util.Date display in a JFrame with always being refreshed to view the new date.
package pro;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class Date {
public static Timer t;
public static void main(String [] args){
time();
}
public static Timer time(){
t = new Timer(1000, new ActionListener(){
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, new java.util.Date().toGMTString());
}
});
t.setRepeats(true);
t.start();
return t;
}
}
I guess I made something wrong in the timer method
I also tried to edit it
package pro;
import java.awt.FlowLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class date {
private static JLabel l;
private static Date d = new Date();
private static JFrame f;
@SuppressWarnings("deprecation")
public static void main(String [] args){
f = new JFrame("Date program");
f.setVisible(true);
f.pack();
f.revalidate();
f.setLayout(new FlowLayout(FlowLayout.LEFT));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l = new JLabel(d.toGMTString());
f.add(l);
while(true){
l.revalidate();
}
}
}
any replies will be appreciated.