2
我试图让JLabel显示日期和每秒更新一次。为此,我使用了Swing的Timer类,并实现了我自己的类DateTimer。 DateTimer也是一个ActionListener。摆动计时器和ActionEvents的问题
这是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());
}
}
我在代码中调用这个从别的地方是这样的:
new DateTimer(1000, this.label);
我得到的日期显示一次,那么它不会永远更新。
我是新来的Java GUI和处理操作,所以请原谅我的无知。
+1,而是他可能会想要做以下'Calendar.getInstance()。getTime()。toString()' – mre 2011-06-13 16:09:10
是的,它做到了。感谢你们俩。我使用Calendar.getInstance()。getTime()。toString() – n0pe 2011-06-13 16:11:19