我希望有一个时钟显示当前时间并每秒刷新一次.我使用的代码是:
int timeDelay = 1000;
ActionListener time;
time = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
timeLabel.setText(DateTimeUtil.getTime());
/*timeLabel is a JLabel to display time,
getTime() is samll static methos to return formatted String of current time */
}
};
SwingWorker timeWorker = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
new Timer(timeDelay, time).start();
return null;
}
};
timeWorker.execute();
Run Code Online (Sandbox Code Playgroud)
我想timeLabel在EDT以外的其他线程中刷新文本.
我做得对吗?还有其他更好的办法?
另外,对于信息,我已添加timeLabel到包含几个类似实用程序的a中,并在另一个中调用.extendedJPanelMainJFrame