在Java中退出计时器

时间:2015-07-22 21:31:50

标签: java swing timer passwords

在我的Java应用程序中,我有一个计时器来跟踪某人不扫描QR码的时间。 20秒后,我抛出一条消息告诉他们使用手动输入而不是扫描QR码。但是,如何在计时器启动后停止计时器?例如,用户前往QR扫描面板。 20秒计时器启动。用户成功扫描QR码。现在我怎么能停止计时器?计时器在我的代码中就像这样。

Timer timer = new Timer();

        timer.schedule( new TimerTask(){

        public void run() { 
               //delay = 20000;

               // if the timer has not been stopped, proceed with timer
               if(!SpectroClick.stop_timer){
                   //System.out.println("20 seconds later");

                    //scanQRPanel.getCameraView().getWebcamPanel().pause();
                    stopScanning();

                    // Create the message to display
                    String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
                    String message = "<html><div style=\"text-align: center;\">" + text + "</html>";

                    // Show the confirmation dialog screen, and wait for user input
                    int i = JOptionPane.showConfirmDialog((Component) null, message,
                            "Confirm", JOptionPane.OK_CANCEL_OPTION);

                    // User selected 'OK'
                    if (i == JOptionPane.OK_OPTION) {
                        for (SpectroClickEventListener listener : listeners) {
                            listener.userSelectedOverride();
                        }
                    }                       

                    // User did not select ok, go to new test
                    else{                           
                        startScanning();
                    }
               }
            }
         }, delay);

我有一个条件来检查是否正在使用定时器,但有没有办法向定时器抛出某种异常?

2 个答案:

答案 0 :(得分:2)

问题已经回答here

您正在寻找的方法是&#34;取消()&#34;和&#34; purge()&#34;。

我把它们扔进了你的代码,但还没有机会运行它:

Timer timer = new Timer();

        timer.schedule( new TimerTask(){

        public void run() { 
               //delay = 20000;

               // if the timer has not been stopped, proceed with timer
               if(!SpectroClick.stop_timer){
                   //System.out.println("20 seconds later");

                    //scanQRPanel.getCameraView().getWebcamPanel().pause();
                    stopScanning();

                    // Create the message to display
                    String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
                    String message = "<html><div style=\"text-align: center;\">" + text + "</html>";

                    // Show the confirmation dialog screen, and wait for user input
                    int i = JOptionPane.showConfirmDialog((Component) null, message,
                            "Confirm", JOptionPane.OK_CANCEL_OPTION);

                    // User selected 'OK'
                    if (i == JOptionPane.OK_OPTION) {
                        for (SpectroClickEventListener listener : listeners) {
                            listener.userSelectedOverride();
                        }
                    }                       

                    // User did not select ok, go to new test
                    else{                           
                        startScanning();
                    }
               } else{
                  timer.cancel();
                  timer.purge();
               }
            }
         }, delay);

答案 1 :(得分:2)

你不应该使用java.util.Timer,而是使用Swing Timer,因为你违反了当前方法的Swing单线程规则。

可以为单次运行配置Swing Timer,这意味着您无需担心将其自行停止

有关详细信息,请查看Concurrency in SwingHow to use Swing Timers