[英]How to stop a Swing Timer?
我的代碼有問題,因為我無法停止swing.timer
//一些代碼
我試圖放一個if語句,但是它不起作用。
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
我想在達到給定值時停止計時器,並在JFrame中將其刪除
您的計時器是最終的
t.cancel();
t.purge();
並在聲明計時器時將其設置為最終計時器
final Timer t = new Timer(...)
您應該按照以下步驟更改代碼:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.