简体   繁体   English

JAVA SWING - 使用 Swing Timer 创建动画

[英]JAVA SWING - Creating Animation using Swing Timer

I am trying to setup a program that enables the user to display a transition when clicking the next and previous button.我正在尝试设置一个程序,使用户能够在单击下一个和上一个按钮时显示转换。 When pressing next, the swing timer should trigger and start the animation.当按下下一步时,摆动计时器应触发并开始动画。 When transitioning, there should be a flag that states it is in the transition period.过渡时,应该有一个标志,表明它处于过渡期。 The Swing timer should fire once every tenth of a second and essentially last 1 second. 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());
        }
    }

}

This: "The Swing timer should fire once every tenth of a second ..." -- does not agree with this: timer = new Timer(1000, this);这个: "The Swing timer should fire once every tenth of a second ..." ——不同意这一点: timer = new Timer(1000, this); Your Timer is firing once every second, not every 10th of a second.您的计时器每秒触发一次,而不是每 10 秒触发一次。

Instead, you should:相反,您应该:

  • Create a new Timer(100, ...) , one that fires every 10th of a second创建一个new Timer(100, ...) ,每 10 秒触发一次
  • Store in an instance field the start time in msecs when the Timer begins (likely do this in your button's ActionListener)将计时器开始时的开始时间(以毫秒为单位)存储在实例字段中(可能在按钮的 ActionListener 中执行此操作)
  • Within the Timer's ActionListener get the current mSecs and use this to check the elapsed time在 Timer 的 ActionListener 中获取当前毫秒并使用它来检查经过的时间
  • Stop the Timer via ((Timer) e.getSource()).stop();通过((Timer) e.getSource()).stop(); once 1 full second has elapsed一旦 1 秒过去了
  • No need for a flag, since all you need to do is to check if the Timer isn't null and if it .isRunning() .不需要标志,因为您需要做的就是检查 Timer 是否为空以及它是否为.isRunning() eg, if (timer != null && timer.isRunning()) { -- then the animation is proceeding.例如, if (timer != null && timer.isRunning()) { -- 然后动画正在进行。

Unrelated suggestion:不相关的建议:

  • Get out of the static world and into the instance world.走出静态世界,进入实例世界。 You're programming in Java, a language that is built to use OOPs from the ground up, and you don't want to fight against the OOPs paradigm.您正在使用 Java 进行编程,Java 是一种从头开始构建使用 OOP 的语言,并且您不想与 OOP 范式作斗争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM