因此,所有在类级别声明的变量都是共享的。您的第一个调用
animateCard将设置它们,然后第二个调用
animateCard将完全覆盖以前的值。您需要将它们从类变量更改为动画的参数。
创建一个新类
AnimationTask,该类实现
ActionListener并保存该类中的变量。
例如,
class AnimationTask implements ActionListener { private int cardID; private int xDest; private int yDest; private int totalXDistance; private int totalYDistance; public AnimationTask(int cardID, int xDest, int yDest) { this.cardID = cardID; this.xDest = xDest; this.yDest = yDest; this.totalXDistance = Math.abs(xDestination - START_POSITION_X); this.totalYDistance = Math.abs(yDestination - START_POSITION_Y); } public void actionPerformed(ActionEvent e) { // do your animation logic... }}并在“动画师”中使用该自定义类
例如,
Timer animator = new Timer(15, new AnimationTask(cardId, xDest, yDest);



