Java 摆动定时器中断 [英] Java swing timer break

查看:30
本文介绍了Java 摆动定时器中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到这一点?这是我正在制作的模拟器.我想要它,以便当计数器(numTimes)达到 139 时,它会停止计时器.我尝试在 Action Listener 之前声明但没有初始化计时器并在 actionPerformed 函数中停止计时器,但它给了我一个错误.我不想使用另一种方法(但如果它效果更好,那么我完全支持它),并且底部的 while 循环导致程序冻结?我怎样才能做到这一点?

Is there any way to do this? This is for a simulator I'm making. I want it so that when the counter(numTimes) reaches 139, it stops the timer. I tried delcaring but no initializing the timer before the Action Listener and stopping the timer within the actionPerformed function, but it gave me an error. I don't want to use another method(but if it works better then I'm all for it), and the while loop at the bottom causes the program to freeze? How can I make this work?

ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    numTimes++;
                    switch(m1values[numTimes])
                    {
                        case 1:
                            new Moves().L();
                            break;
                        case 2:
                            new Moves().Lprime();
                            break;
                        case 3:
                            new Moves().R();
                            break;
                        case 4:
                            new Moves().Rprime();
                            break;
                        case 5:
                            new Moves().F();
                            break;
                        case 6:
                            new Moves().Fprime();
                            break;
                        case 7:
                            new Moves().B();
                            break;
                        case 8:
                            new Moves().Bprime();
                            break;
                        case 9:
                            new Moves().U();
                            break;
                        case 10:
                            new Moves().Uprime();
                            break;
                        case 11:
                            new Moves().D();
                            break;
                        case 12:
                            new Moves().Dprime();
                            break;
                        default:    
                    }
                    drawAndButtons.add(new graphics());
                    cubeSpace.repaint();
                    if(numTimes >= 139)
                    {
                        numTimes = 0;
                        m1going = false;
                    }
                }
            };
            Timer timer = new Timer( 500 , taskPerformer);
            timer.setRepeats(true);
            timer.start();
            startTime = System.currentTimeMillis();
            m1going = true;
            while(m1going = true)
            {}
            timer.stop();

推荐答案

while 循环的问题是如果在 EDT 的上下文中执行,它会阻止定时器被触发

The problem with the while loop is if it's executed within the context of EDT, it will stop the timer from been triggered

相反,摆脱循环并停止 ActionListener

Instead, get rid of loop and stop the Timer within the ActionListener

            public void actionPerformed(ActionEvent evt) {
                numTimes++;
                //...
                if(numTimes >= 139)
                {
                    numTimes = 0;
                    ((Timer)evt.getSource()).stop();

这篇关于Java 摆动定时器中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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