[英]JAVA SWING - Creating Animation using Swing Timer
我正在尝试设置一个程序,使用户能够在单击下一个和上一个按钮时显示转换。 当按下下一步时,摆动计时器应触发并开始动画。 过渡时,应该有一个标志,表明它处于过渡期。 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());
}
}
}
这个: "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();
一旦 1 秒过去了.isRunning()
。 例如, if (timer != null && timer.isRunning()) {
-- 然后动画正在进行。不相关的建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.