简体   繁体   English

Java Swing计时器休息

[英]Java swing timer break

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. 我想要这样,以便当计数器(numTimes)达到139时,它将停止计时器。 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. 我尝试了delcaring,但是没有在Action Listener之前初始化计时器,也没有在actionPerformed函数中停止计时器,但是它给了我一个错误。 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? 我不想使用其他方法(但是如果效果更好,那么我全力以赴),而底部的while循环会导致程序冻结? 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();

This is a 1-character typo. 这是1个字符的错字。 The while loop will never stop, because you typed = instead of == . while循环永远不会停止,因为您键入=而不是== The reason this even compiles at all is because the expression m1going = true returns the value of true as well as assigning true to m1going . 甚至编译的原因是因为表达式m1going = true返回值true以及将true m1going The while loop requires a boolean, and that's what it gets. while循环需要一个布尔值,这就是它的含义。

This is a common error in C/C++, because an int is a valid boolean. 这是C / C ++中的常见错误,因为int是有效的布尔值。 Normally, Java will catch this for you, and complain that you can't put an int (or whatever) in a while loop, but if you make this mistake with a boolean , it is not caught. 通常,Java会为您捕获此错误,并抱怨您无法在while循环中放入int (或其他任何内容),但是如果您使用boolean犯此错误,则不会捕获该错误。

The problem with the while loop is if it's executed within the context of EDT, it will stop the timer from been triggered while循环的问题是,如果它在EDT的上下文中执行,它将停止计时器的触发

Instead, get rid of loop and stop the Timer within the ActionListener 相反,摆脱循环并在ActionListener停止Timer

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

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

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