JAVA SWING-使用Swing计时器创建动画 [英] JAVA SWING - Creating Animation using Swing Timer

查看:102
本文介绍了JAVA SWING-使用Swing计时器创建动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个程序,使用户可以在单击下一个和上一个按钮时显示过渡.当按下next时,swing计时器应触发并开始动画.过渡时,应该有一个标志指出它处于过渡期间.Swing计时器应每十分之一秒触发一次,并且基本上应持续1秒钟.

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.

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());
        }
    }

}

推荐答案

此:"Swing计时器应每十分之一秒触发一次……" -与此不同: timer = new Timer(1000,this); 您的计时器每秒触发一次,而不是每10秒触发一次.

This: "The Swing timer should fire once every tenth of a second ..." -- does not agree with this: timer = new Timer(1000, this); Your Timer is firing once every second, not every 10th of a second.

相反,您应该:

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

不相关的建议

  • 脱离静态世界,进入实例世界.您正在用Java编程,而Java是一种从头开始使用OOP的语言,并且您不想与OOP范式作斗争.

这篇关于JAVA SWING-使用Swing计时器创建动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆