gpt4 book ai didi

java - 定时器程序卡住

转载 作者:行者123 更新时间:2023-11-29 07:51:27 25 4
gpt4 key购买 nike

我正在开发一个 JFrame 定时器,每次我按下开始程序都会卡住 这是我的代码:

public void startTime() throws InterruptedException{
Thread thread = new Thread();
for(int i = 0;i<10000;i++){
seconds++;
timeLabel.setText(hours+" : "+ minutes +" : "+seconds);
switch(seconds){
case 60:
seconds = 0;
minutes++;
break;
}
switch(minutes){
case 60 :
minutes = 0;
seconds = 0;
hours++;
break;
}
thread.sleep(700L);
}
}
@Override
public void actionPerformed(ActionEvent e) {
try {
startTime();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

我正在尝试这样做,以便当我按下按钮时它会启动计时器关于如何解决这个问题的任何想法?

最佳答案

您在 Swing 事件线程上调用 Thread.sleep。

这个:

Thread thread = new Thread();

什么都不做。

  • 考虑在其中放置一个 Runnable,然后在该 Runnable 中完成您的非 EDT 工作。
  • 或者甚至更好地使用 Swing 计时器。

关于java - 定时器程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20810398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com