这个计时器出了什么问题?

时间:2014-01-13 02:30:22

标签: java timer countdown

我正在做一个计时器倒计时90秒一直到零,但是当我运行它时,它将运行1秒并终止,PLZ帮助!指出什么是错的!

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 ();
    }
}

2 个答案:

答案 0 :(得分:3)

(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();
        }

    }

}

答案 1 :(得分:2)

“然而,当我运行它时,它将运行1秒并终止”

因为你在这里设置为1秒(1000毫秒):

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

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

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