简体   繁体   English

Java awt swing,带有计时器的开始/停止按钮

[英]Java awt swing, start/stop button with timer

I have a simple application that changes the color of a label when an action is performed. 我有一个简单的应用程序,可以在执行操作时更改标签的颜色。

When I click the button for my application(the action being performed), the color of the text of the label will continuously change on an interval set by the timer. 当我单击应用程序的按钮(正在执行的操作)时,标签文本的颜色将按计时器设置的间隔连续变化。

Now, what I am trying to accomplish is to have my button have the functionality of starting and stopping the change in color for the text of the label. 现在,我要完成的工作是使按钮具有启动和停止标签文本颜色更改的功能。

I am able to have it start, but I think there is a flaw in my logic when trying to stop execution of the change in color. 我可以启动它,但是我认为尝试停止执行颜色更改时逻辑上存在缺陷。

public void actionPerformed(ActionEvent e) 
    {   
        Timer timer = new Timer(500, timerListener);
        while(!timer.isRunning())
        {
            if(!timer.isRunning())
            {
                button.setText("Stop");
                timer.start();
            }
            else
            {
                timer.stop();
                button.setText("Start");
            }
}

Without the contents following the "else" I am able to start the program, but not stop it. 没有“ else”之后的内容,我可以启动程序,但不能停止它。

Please review and help. 请查看并提供帮助。 Thanks! 谢谢!

Well, the code above creates a new timer each time the method is invoked. 好的,上面的代码每次调用该方法时都会创建一个新的计时器。 So the reference to the old timer is lost, which is why calling stop() has no effect. 因此,对旧计时器的引用丢失了,这就是为什么调用stop()无效的原因。

(Sorry for also posting this as a comment, but after I typed it all in I realized it might be the entire answer, not just a note about programming style. So I added it as an answer.) (很抱歉也将其作为注释发布,但是在我全部键入后,我意识到这可能是完整的答案,而不仅仅是关于编程风格的注释。因此,我将其添加为答案。)

For comparison, here's some working code of mine that does something similar. 为了进行比较,这是我的一些类似的工作代码。 Notice I only create a new timer in the case where I'm going to be starting a new timing event. 注意,只有在要开始新的计时事件的情况下,我才创建一个新的计时。 The stop case just stops the existing timer, it never creates one. 停止条件只会停止现有的计时器,而不会创建一个计时器。

  @Override
  public void actionPerformed( ActionEvent e )
  {
     if( timing ) {
        timing = false;
        timer.stop();
        view.setStartButtonText( "start stting" );
        view.setTimerLabel( "stopped" );
        view.setBackground( Color.CYAN.darker() );
     } else {
        nanoTime = System.nanoTime();
        view.setStartButtonText( "stop stting" );
        timing = true;
        timer = new javax.swing.Timer( 1000,
                new ActionListener()
                {
                   @Override
                   public void actionPerformed( ActionEvent e )
                   {
                      long minuteTime = ( System.nanoTime() -
                      nanoTime ) /
                      ( 60_000_000_000L );
                      view.setTimerLabel( minuteTime + " minutes" );
                      if( minuteTime > 50 )
                         if( darkBg ) {
                            view.setBackground( Color.PINK );
                            darkBg = false;
                         } else {
                            view.setBackground( Color.PINK.
                                    darker() );
                            darkBg = true;
                         }
                   }
                } );
        timer.setRepeats( true );
        timer.start();
     }
  }

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

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