我想在每次點擊一個按鈕時在一個小程序中執行一個動畫。我第一次點擊按鈕一切正常。但第二次,動畫的速度在增加。第三次動畫的速度增加了一點點,第四次,第五次......每當我點擊一個按鈕,計時器就會增加速度
我不知道計時器發生了什麼。我該如何解決它?
在小程序我使用此代碼:
JButton btnIniciar = new JButton("Iniciar");
btnIniciar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Timer timer = new Timer(50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
//I have a list of packages to animate
for (Package p: listaPaquetes){
p.animate();
panel.repaint();
}
}
});
timer.start();
}
這是重繪的面板代碼:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//I use the same list of the applet
for (Package p: listaPaquetes){
//Paint the package
p.paintPackage(g);
}
}
This is how it works, the animation sends packages from left to right
已解決。我不知道把計時器停在哪裏,所以每次按下按鈕時,我也停止計時器,除了第一次。 – carlbron