简体   繁体   English

如何使用“ if”停止计时器

[英]how to stop a timer with 'if'

hi guys i am new to java... :( i just want my button(start) to start a timer but i want to stop the timer automatically with 'if' so for example... when a user enters a time and the timer get's to the timer it stops... so far my coding is like this... 嗨,大家好,我是java的新手... :(我只是想让我的button(start)启动一个计时器,但是我想用'if'自动停止计时器,例如...当用户输入时间,计时器获取到它停止的计时器...到目前为止,我的编码是这样的...

private void startTimerActionPerformed(java.awt.event.ActionEvent evt) {                                               

        javax.swing.Timer tm = new javax.swing.Timer(100, new ActionListener()
        {
            public void actionPerformed (ActionEvent evt) {
                AddOneActionPerformed(evt);
            }   
        });

        tm.start();

        int getTM,getM,getTS,getS,Secs,tenSec,Mins,tenMin;

        getTM = Integer.parseInt(enterTenMins.getText());
        getM = Integer.parseInt(enterOneMins.getText());
        getTS = Integer.parseInt(enterTenSecs.getText());
        getS = Integer.parseInt(enterOneSecs.getText());

        tenMin = Integer.parseInt(tenMins.getText());
        Mins = Integer.parseInt(oneMins.getText());
        tenSec = Integer.parseInt(tenSecs.getText());
        Secs = Integer.parseInt(oneSecs.getText());
    }                                    

and AddOneActionPerformed(evt) is AddOneActionPerformed(evt)

private void AddOneActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int dd,Secs,tenSec,Mins,tenMin;

        tenMin = Integer.parseInt(tenMins.getText());
        Mins = Integer.parseInt(oneMins.getText());
        tenSec = Integer.parseInt(tenSecs.getText());
        Secs = Integer.parseInt(oneSecs.getText());
        dd= Integer.parseInt(digitValue.getText());
        dd= dd+1;


        if (dd==10)
            dd = 0;

        if (Secs == 10)
            Secs = 0;

        if (dd==0)
            Secs=Secs +1;

        if (tenSec>=6)
            tenSec = 0;

        if (Secs==10)
            tenSec=tenSec +1;

        if (Mins==10)
            Mins = 0;

        if (tenSec==6)
            Mins=Mins+1;

        if (tenMin>=6)
            tenMin=0;

        if (Mins==10)
            tenMin=tenMin+1;

        String ss = Integer.toString(dd);
        digitValue.setText(ss);

        String ff = Integer.toString(Secs);
        oneSecs.setText(ff);

        String gg = Integer.toString(tenSec);
        tenSecs.setText(gg);

        String hh = Integer.toString(Mins);
        oneMins.setText(hh);

        String jj = Integer.toString(tenMin);
        tenMins.setText(jj);

        showDigitActionPerformed(evt);
        showOneSecsActionPerformed(evt);
        showTenSecsActionPerformed(evt);
        showOneMinsActionPerformed(evt);
        showTenMinsActionPerformed(evt);

    }

You can get the Timer instance from the ActionEvent's getSource() method, and then call stop on it. 您可以从ActionEvent的getSource()方法获取Timer实例,然后在其上调用stop。 So,... 所以,...

// for your Timer's ActionListener
@Override
public void actionPerformed(ActionEvent evt) {
   if (someStoppingConditionIsTrue) {
      Timer timer = (Timer) evt.getSource();
      timer.stop();
   } else {
      // code to call repeatedly
   }
}

Note that you've got two separate issues going on, and should solve them separately. 请注意,您遇到了两个单独的问题,应该分别解决。 The code above is a way to stop a Timer from within its own ActionListener using some condition. 上面的代码是一种使用某些条件从其自己的ActionListener中停止Timer的方法。 Your other question is how to get user input to allow you to change that condition, and that will involve separate code that is independent of the Timer code above. 您的另一个问题是如何获取用户输入以允许您更改该条件,而这将涉及独立于上面的Timer代码的单独代码。

Consider in addtion: 另外考虑:

  • Use two JSpinners to for the user's to input minutes and seconds. 使用两个JSpinners作为用户输入分钟和秒。
  • Give your class an int totalTime field. 给您的班级一个int totalTime字段。
  • Set this value in your startTimerActionPerformed method. 在您的startTimerActionPerformed方法中设置此值。
  • Have the Timer's ActionListener decrement this value based on measured elapsed time using the differences in calls to System.getSystemTime(). 使用对System.getSystemTime()的调用中的差异,让Timer的ActionListener根据测量的经过时间将该值减一。
  • Calculate get the difference between the totalTime and elapsedTime (it will be in milliseconds), say called timeLeft 计算totalTime和elapsedTime之间的差(以毫秒为单位),称为timeLeft
  • Calculate your minutes and seconds from timeLeft. 从timeLeft计算您的分钟和秒钟。
  • Then display these values in a JLabel using a formatted String, say something like String.format("%02d:%02d", minutes, seconds) . 然后使用格式化的String在JLabel中显示这些值,例如String.format("%02d:%02d", minutes, seconds)
  • When the timeLeft == 0, that's when you stop your Timer. 当timeLeft == 0时,就是您停止计时器的时间。

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

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