【问题标题】:How to stop timer in a swing application如何在摇摆应用程序中停止计时器
【发布时间】:2016-10-12 15:54:25
【问题描述】:

我写了一个小代码来改变几个按钮的颜色来激发随机序列的闪烁颜色。我已经为我设置了一个计时器,但我只是不知道如何停止它

这是我的代码

          Timer timer = new Timer(100, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int buttons[] = new int[9];

                        for (int j = 0; j < buttons.length; j++) {

                        //  System.out.println("times");
                            Component button = frame.getContentPane().getComponent(j);
                            int Red = new Random().nextInt(255 - 1) + 1;
                            int Green = new Random().nextInt(255 - 1) + 1;
                            int Blue = new Random().nextInt(255 - 1) + 1;

                            button.setBackground(new Color(Red, Green, Blue));

                            SwingUtilities.updateComponentTreeUI(button);

                        }

                    }// }

                }); timer.start();

【问题讨论】:

  • 您需要在 Timer 对象上调用 stop() 方法。您希望它何时/如何停止?
  • 创建一个变量。然后在定时器滴答时增加它。当变量值达到8时停止定时器
  • @fastsnil 我试图创建一个整数变量 counter 并在 for 循环中递增它但不起作用,你能告诉我你的代码是什么意思吗?
  • 对于example
  • 不要使用updateComponentTree(...)!!!没有必要使用这种方法。您需要做的就是调用 setBackground() 方法,组件将自行重新绘制。 increment it in the for loop but doesn't work, - 不,你不会在 for 循环中增加它。在 actionPerformed() 方法的末尾增加它。然后当达到 10 时停止计时器。

标签: java swing timer timertask


【解决方案1】:

有几种方法可以做到这一点,这里有一些快速的方法。

一种方法是利用System.currentTimeMillis():

private Timer timer;
private final int DELAY = 100;
private final int DURATION = 10_000;
private long startTime;

public void timerExample() {
    timer = new Timer(DELAY, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (System.currentTimeMillis() >= startTime + DURATION) {
                System.out.println("Done");
                timer.stop();
            } else {
                System.out.println("Tick");
            }
        }
    });
    startTime = System.currentTimeMillis();
    timer.start();
}

如果当前时间大于startTime + DURATION,这将停止Timer

另一种方法是使用计数器:

private Timer timer;
private final int DELAY = 100;
private final int DURATION = 10_000;
private final int TOTAL_TICKS = DURATION / DELAY;
private int tickCounter = 0;

public void timerExample() {
    timer = new Timer(DELAY, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (++tickCounter == TOTAL_TICKS) {
                System.out.println("Done");
                timer.stop();
            } else {
                System.out.println("Tick");
            }
        }
    });
    timer.start();
}

DELAY 为 100 毫秒,DURATION 为 10,000 毫秒,您可以计算所需的计时器 滴答声 总数,例如10,000 / 100 = 100 个滴答声。

每次触发动作侦听器时,它都会检查是否达到了总滴答数,如果达到则停止计时器。

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多