摆动计时器不停止



我使用swing。我所有的课都有计时器,我在所有计时器中都遇到了同样的问题问题是:我的所有计时器第一次都工作良好,但第二次所有计时器都没有停止

这是我所有计时器的代码

   Timer timer1;
   S= 0 ;
   ActionListener taskPerformer2 ; = new ActionListener() { 
    public void actionPerformed(ActionEvent evt) {                  
        if (  S == 10 ){            
           // My work   
           timer1.stop(); 
        }       
        S++;
        System.out.println(S + "A");
    }; 
 };
 timer1 = new Timer(20, taskPerformer2);
 timer1.start();

直接从事件中检索当前计时器。

public void actionPerformed(ActionEvent evt) {                  
  if (  S == 10 ){            
       // My work   
       ((Timer)evt.getSource()).stop(); // <-- this was changed
    }       
    S++;
    System.out.println(S + "A");
  }; 

最新更新