1 java swing timer jpanel jframe
出于某种原因,即使我使用oracle网站上的确切代码示例来进行Swing Timer,它也不会等待1秒钟.它只是跳到了"你的分数等等"的JOptionPane.
这是我的学校项目的源代码.在运行剩下的代码之前,为什么这不工作而不等待1秒?
//Check to see if user has enetered anything
if(!answered)
{
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
afk = true;
incorrect += 1;
answered = true; //This breakes it out of the loop
}
Run Code Online (Sandbox Code Playgroud)
计时器用于在特定时间后运行回调.如果你只是想拖延,你可以移动代码的延迟后运行到的taskPerformer动作侦听器.
Thread.sleep(1000) 这里并不理想,因为它会导致UI完全冻结,因为你会让UI线程睡眠.
| 归档时间: |
|
| 查看次数: |
332 次 |
| 最近记录: |