【问题标题】:What is wrong with this timer?这个计时器有什么问题?
【发布时间】:2014-01-13 02:30:21
【问题描述】:

我正在做一个计时器,从 90 秒一直倒计时到零,但是当我运行它时,它将运行 1 秒并终止,请帮助!指出问题所在!

package TestingFile;

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;


public class TestingATimer extends JFrame
{
    private Timer timer;
    public int count = 90;

    public TestingATimer()
    {
        timer = new Timer(1000, new TimerListener());
        timer.start();
    }

    private class TimerListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            if (count != 0)
            {   
            count--;
            System.out.println(count + " seconds elapsed"); 
            }
        }

    }

    public static void main (String [] args)
    {
        new TestingATimer ();
    }
}

【问题讨论】:

  • 嗯。也许是因为您将其设置为 1000 毫秒?

标签: java timer countdown


【解决方案1】:

(Swing)Timer 可能使用基于daemon 的线程。这意味着一旦main 方法存在,就没有什么可以阻止JVM 终止...

来自Thread JavaDocs

当 Java 虚拟机启动时,通常有一个 非守护线程(通常调用一些名为 main 的方法 指定类)。 Java 虚拟机继续执行 线程,直到发生以下任一情况:

  • Runtime 类的退出方法已被调用且安全性 经理已允许进行退出操作。
  • 所有线程 不是守护线程的线程已经死亡,要么从 调用 run 方法或抛出异常传播 超越 run 方法。

所以没有什么可以阻止 JVM 终止。

问题是,你为什么要使用没有 GUI 的javax.swing.Timer?你想达到什么目的?

更新

如果您不想使用 GUI,则需要使用java.util.Timer,例如...

import java.util.Timer;
import java.util.TimerTask;

public class Clock {

    private static Timer timer;

    public static void main(String[] args) {

        timer = new Timer();
        timer.scheduleAtFixedRate(new TickTask(), 0, 1000);

    }

    public static class TickTask extends TimerTask {

        private boolean started = false;
        private long startTime = 0;

        @Override
        public void run() {
            if (!started) {
                started = true;
                startTime = System.currentTimeMillis();
            } else {
                long dif = System.currentTimeMillis() - startTime;
                long seconds = dif / 1000;
                System.out.println(seconds);
                if (seconds >= 90) {
                    timer.cancel();
                }
            }
        }

    }

}

否则你需要提供某种 GUI...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClockGUI {

    public static void main(String[] args) {
        new ClockGUI();
    }

    public ClockGUI() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Timer timer;
        private JLabel clock;

        public TestPane() {
            setLayout(new GridBagLayout());
            clock = new JLabel("...");
            add(clock);
            timer = new Timer(1000, new ActionListener() {

                private boolean started = false;
                private long startTime = 0;

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!started) {
                        started = true;
                        startTime = System.currentTimeMillis();
                    } else {
                        long dif = System.currentTimeMillis() - startTime;
                        long seconds = dif / 1000;
                        clock.setText(Long.toString(seconds));
                        if (seconds >= 90) {
                            timer.stop();
                        }
                    }
                }
            });
            timer.start();
        }

    }

}

【讨论】:

  • “问题是,你为什么要使用没有 GUI 的 javax.swing.Timer?你想达到什么目的?” -- 完全正确!!
【解决方案2】:

" 但是当我运行它时,它将运行 1 秒并终止"

因为您在此处将其设置为 1 秒(1000 毫秒):

timer = new Timer(1000, new TimerListener());

如果您想要 90 秒,请执行以下操作:

timer = new Timer(90000, new TimerListener());

【讨论】:

  • 不,它只是终止。
  • @user3171151 “nope”指的是哪一部分?
  • 什么时候,我把它改成90000。
  • @user3171151 尝试改用timer.schedule(new TimerListener(), 90000);
  • @RafaEl 在javax.swing.Timerjava.util.Timer 中都没有这种方法...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
相关资源
最近更新 更多