i have a problem in my code, because i cant stop the swing.timer
//some code here
I tried to put a if statement but it doesnt working.
Static int gScore = 1000;
t = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
score01.setText("score: " + gScore);
gScore--;
}
});
t.start();
t.setRepeats(true);
if(gScore== 970){//A
t.stop();
t.setRepeats(false);
}//A
i want to stop the timer when it reach to a given value and remove it inside the JFrame
Your timer shall be final
t.cancel();
t.purge();
and when declaring your timer, set it as final
final Timer t = new Timer(...)
You should change your code as followed:
static int gScore = 1000;
final Timer t = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
score01.setText("score: " + gScore);
gScore--;
if(gScore== 970){//A
t.stop();
}//A
}
});
t.start();
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.