JAVA SWING使用SWING计时器创建动画
我正在尝试设置一个程序,使用户可以在单击“下一步”和“上一步”按钮时显示转换。按下“下一步”时,摆动计时器将触发并启动动画。当过渡时,应该有一个标志表明它处于过渡期。摆动计时器应每十分之一秒触发一次,基本上持续1秒
public class guiCreation {
static Timer timer;
static boolean flag = false;
private static void guiInterface() {
next.addActionListener(new ActionListener(){
timer = new Timer(1000, this);
public void actionPerformed(ActionEvent e){
nextGest();
}
});
//should go to the next tab
previous.addActionListener(new ActionListener(){
//if the list gets to the beginning, disable button
public void actionPerformed(ActionEvent e){
prevGest();
}
});
}
public static void nextGest() {
timer.start();
previous.setEnabled(true);
next.setEnabled(true);
//if the list gets to the end, disable button
if (cardLayout.isNextCardAvailable()) {
status.setText(" Next button has been clicked");
//System.out.println("This is the" + size);
cardLayout.next(cardPanel);
next.setEnabled(cardLayout.isNextCardAvailable());
}
}
public static void prevGest() {
if (cardLayout.isPreviousCardAvailable()) {
timer.start();
next.setEnabled(true);
previous.setEnabled(true);
status.setText(" Previous button has been clicked");
cardLayout.previous(cardPanel);
previous.setEnabled(cardLayout.isPreviousCardAvailable());
}
}
}
# 1 楼答案
此:
"The Swing timer should fire once every tenth of a second ..."
与此不一致:timer = new Timer(1000, this);
您的计时器每秒触发一次,而不是每10秒触发一次相反,你应该:
new Timer(100, ...)
,每10秒触发一次((Timer) e.getSource()).stop();
停止计时器.isRunning()
。e、 然后动画正在进行李>无关建议: