简体   繁体   English

多次调用startTimer方法(Java)

[英]Calling startTimer Method Multiple Times(Java)

So I've been browsing the web for answers but feel overwhelmed and can't seem to find an explanation of this. 因此,我一直在浏览网络以查找答案,但感到不知所措,似乎找不到这种解释。 For one of my classes we were given an example with code very similar to what I'm using and I copied it (dangerous I know but not sure how else to go about it.) 对于我的一个类,我们给出了一个示例,该示例的代码与我正在使用的代码非常相似,并且我将其复制了(很危险,我知道但不确定该如何做。)

I'm trying to make it so that when a button is pressed a JLabel reads a certain text and 5 seconds later that text disappears. 我试图做到这一点,以便当按下按钮时,JLabel会读取特定的文本,并且5秒钟后该文本会消失。 I have been able to do this successfully, but there is a catch. 我已经能够成功完成此操作,但是有一个问题。 I have 6 JButtons that share the same ActionListener which can, under the right conditions, call the startStatusTimer method. 我有6个JButton,它们共享同一个ActionListener,可以在适当的条件下调用startStatusTimer方法。

If one button is pressed, and within 5 seconds another button is pressed, it starts another timer, and the more buttons I press the more timers I get that never seem to never stop. 如果按下一个按钮,并在5秒钟内按下另一个按钮,它将启动另一个计时器,而我按下的按钮越多,我得到的计时器就越多,而且永远不会停止。 The text eventually goes away, but only after the 5 seconds has passed from the the first button is pressed. 文本最终消失,但仅在从第一个按钮按下5秒钟之后。

I keep getting count values >5 printing out after pressing buttons multiple times within a few seconds but I do not see how this can be because of the if statement. 在几秒钟内多次按下按钮后,我不断获得计数值> 5的打印输出,但是由于if语句,我看不到这种情况。

It does not seem like good coding practice to leave a bunch of timers running in the background so how do I stop them? 在后台运行一堆计时器似乎不是一种好的编码习惯,那么如何停止它们呢?

private void startStatusTimer()
{
    ActionListener taskPerformer = new ActionListener() 
    {
        int count=0;
        public void actionPerformed(ActionEvent event) 
        {
            count++;
            System.out.println(count);
            if(count>=5)
            {
                statusLabel.setText("");
                statusTimer.stop();
            }
        }
    };
    statusTimer = new Timer(second, taskPerformer);
    statusTimer.start();
}

The System.out.println is simply for debugging. System.out.println仅用于调试。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

It does not seem like good coding practice to leave a bunch of timers running in the background 在后台运行一堆计时器似乎不是一种好的编码习惯

Generally, no, you don't want to have multiple Swing Timer s running if you can help it. 通常,不,如果可以帮助的话,您不想运行多个Swing Timer They won't scale well (the more you add, the worst things become). 它们无法很好地扩展(添加的数量越多,最糟糕的事情就变成了)。 Where you trying to do the same or similar things, try and use as few Timer s as possible. 在尝试执行相同或相似操作的地方,请尝试并使用尽可能少的Timer

Instead, try creating a single instance of the Timer ... 而是尝试创建Timer的单个实例...

 public class ... {
     private Timer wipeOutTimer;

     public ...() {
          wipeOutTimer = new Timer(5000, new ActionListener() {
              public void actionPerformed(ActionEvent event) 
              {
                  statusLabel.setText("");
              }
          });
          wipeOutTimer.setRepeats(false);
    }

Then in your startStatusTimer you can check the state of the Timer ... 然后在您的startStatusTimer您可以检查Timer的状态...

private void startStatusTimer() {
    if (!wipeOutTimer.isRunning()) {
        wipeOutTimer.start();
    }

This will ensure that no matter how many times you press buttons, while there will only ever be a single Timer running and it will only be started once within a 5 second period. 这将确保无论您按多少次按钮,都只会运行一个Timer ,并且仅在5秒钟内启动一次。

so how do I stop them? 那我该如何阻止他们?

This depends on what you are trying to do. 这取决于您要执行的操作。 You could use setRepeats(false) so timer will only run once. 您可以使用setRepeats(false)以便计时器仅运行一次。 You could also use isRunning to check to see if the Timer is already running and even use restart to cause the timer to reset should you want to... 您还可以使用isRunning来检查Timer是否已在运行,甚至还可以使用restart来使计时器重置。

How can I make the timer restart to 5 seconds everytime the button is pressed? 每次按下按钮时如何使计时器重新启动至5秒?

Simply call restart 只需致电restart

private void startStatusTimer() {
    if (!wipeOutTimer.isRunning()) {
        wipeOutTimer.start();
    } else {
        wipeOutTimer.restart();
    }

You can actually just call restart every time, but this illustrates some additional functionality you may no know about... 实际上,您每次都可以调用restart ,但这说明了您可能不知道的一些其他功能...

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

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