简体   繁体   中英

Swing Timer issue - stop Timer

I have been trying to stop the timer and solve this for days now, unfortunately no luck, hope someone will be able to help.

The idea is to use the timer to click a button which increases the value in the text field so I have the timer in the start button and I wish to stop it in the stop button.

That's the code I have behind my start button:

private void btStartTimerActionPerformed(java.awt.event.ActionEvent evt) { 

        javax.swing.Timer tm = new javax.swing.Timer(100, new ActionListener(){
            public void actionPerformed(ActionEvent evt) {

                btAddOneActionPerformed(evt);
               }
        });
tm.start();

 private void btStopTimerActionPerformed(java.awt.event.ActionEvent evt) { 

}

The solution to this is in keeping a reference to the Timer object that is created. For more info. see The Java™ Tutorials - Classes and Objects: Declaring Member Variables .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM