Asi*_*sif 5 java swing swingworker javax.swing.timer
我希望有一个时钟显示当前时间并每秒刷新一次.我使用的代码是:
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
Jon*_*nas 12
你可以在没有SwingWorker的情况下做到这一点,因为这就是Swing Timer的用途.
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 */
}
};
new Timer(timeDelay, time).start();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5024 次 |
| 最近记录: |