gpt4 book ai didi

java - 当我在JAVA上的定时器中时,我可以停止定时器吗

转载 作者:行者123 更新时间:2023-12-02 01:45:47 25 4
gpt4 key购买 nike

我有一个游戏方法,该方法内有计时器,仅适用于某些特定情况(如果条件如下)我想停止计时器...但由于某种原因它导致我崩溃。

public model() {
public game() {
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
....
//draw shapes on JFrame

if (model.Life == 0) { //specific condition
model.timer.stop(); //timer is making a crash here
}
repaint();
}
});
timer.start();
}

最佳答案

计时器是 ActionEvent 的来源,因此您可以执行以下操作:

if (your condition)
{
Timer timer = (Timer)e.getSource();
timer.stop();
}

这样您就不必担心为计时器保留实例变量。

关于java - 当我在JAVA上的定时器中时,我可以停止定时器吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686274/

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