[英]can I stop timer When I am in Timer on JAVA
I have a game method that has Timer inside this method, only for some specific case(if condition below) i wanna stop timer...But for some reason it is causing me a crash. 我有一个游戏方法在此方法内包含Timer,仅在某些特定情况下(如果满足以下条件),我想停止计时器...但是由于某种原因,这导致我当机。
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();
}
The Timer is the source of the ActionEvent so you can just do: 计时器是ActionEvent的来源,因此您可以执行以下操作:
if (your condition)
{
Timer timer = (Timer)e.getSource();
timer.stop();
}
This way you don't have to worry about keeping an instance variable for the Timer. 这样,您不必担心为Timer保留实例变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.