我正在尝试让它在frame.setTitle上显示经过的时间在直播时间,但我有困难找到该做什么,有人可以帮助我吗?这是我的代码
DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss:aa yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
frame.setTitle("[SESSION] - "+ myUsername +" Elapsed Time " + dateFormat.format(cal.getTime()) );
发布于 2020-10-04 11:41:58
我猜你可以创建一个计时器,它每秒执行一次来更新标题(我不确定它的性能如何,但它应该可以完成这项工作)
class UpdateFrame extends TimerTask {
public void run() {
frame.setTitle("[SESSION] - "+ myUsername +" Elapsed Time " + dateFormat.format(cal.getTime()) );
}
}
// And From your main() method or any other method, maybe where you initialize the JFrame
NoiseMap noisemap;
Timer timer = new Timer();
timer.schedule(new UpdateFrame(), 0, 1000);
您还需要传递JFrame对象,可能还需要传递开始计数时的DateFormat或Calendar,我还没有测试过它,但它应该可以工作
class UpdateFrame extends TimerTask {
JFrame frame;
public UpdateFrame(JFrame frame) {
this.frame = frame;
}
public void run() {
frame.setTitle("[SESSION] - "+ myUsername +" Elapsed Time " + dateFormat.format(cal.getTime()) );
}
}
// And From your main() method or any other method, maybe where you initialize the JFrame
NoiseMap noisemap;
Timer timer = new Timer();
timer.schedule(new UpdateFrame(frame), 0, 1000);
https://stackoverflow.com/questions/64098510
复制