n0p*_*0pe 2 java user-interface swing date timer
我正在尝试让JLabel显示日期并每秒更新一次.为此,我使用Swing的Timer类并实现我自己的类DateTimer.DateTimer也是一个ActionListener.
这是DateTimer:
public class DateTimer implements ActionListener {
private int delay;
private JLabel label;
private Calendar cal;
public DateTimer(int delay, JLabel label) {
this.delay = delay;
this.label = label;
cal = Calendar.getInstance();
new Timer(this.delay, this).start();
}
public void actionPerformed(ActionEvent e) {
this.label.setText(this.cal.getTime().toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的代码中的其他地方调用这个,如下所示:
new DateTimer(1000, this.label);
Run Code Online (Sandbox Code Playgroud)
我得到一次显示的日期,然后它不会更新.
我是Java GUI和处理动作的新手,所以请原谅我的无知.