我需要一個計時器在我的應用程序中執行,它將從10秒到0秒進行倒計時。 ,並在JLabel
中顯示倒計時。顯示倒計時
這是我的實現;
...
Timer t = new Timer(1000, new List());
t.start();
}
class List implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int sec = 0;
label.setText(""+sec);
// Do a if- condition check to see if the clock has reached to, and then stop
}
}
我期待着JLabel從0-10開始計數然後停下來。但事實並非如此。 JLabel設置值0
並且它不會遞增。
UPDATE 1
t = new Timer(1000, new Listner());
t.start();
}
class Listner implements ActionListener{
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
lable.setText(""+ (counter++));
if (counter == 10)
t.removeActionListener(this);
}
}
不會從每次調用actionPerformed時計數器從0開始? – Fyre
@Fyre編號'私人計數器= 0;'創建類時創建。在'actionPerformed'中'counter'只是增加了。 –
噢耶我的壞沒有看到它的類變量....謝謝 – Fyre