这个计时器有什么问题? [英] What is wrong with this timer?
问题描述
我正在做一个定时器,将90秒的时间一直递减到零,但是当我运行它时,它将运行1秒钟并终止,请帮助!指出问题所在!
I am doing a timer to countdown fro 90 seconds all the way down to zero, however when i run it, it will run for 1 second and terminate, plz help! Point out what is wrong!
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 ();
}
}
推荐答案
(摆动) Timer
可能使用基于 daemon
的线程。这意味着一旦 main
方法存在,就没有什么可以阻止JVM终止...
The (Swing) Timer
is likely using a daemon
based thread. This means that once the main
method exists, there is nothing keeping the JVM from terminating...
From the Thread
JavaDocs
Java虚拟机启动时,通常只有一个
非守护线程(通常会调用某些
指定类的名为main的方法)。 Java虚拟机将继续执行
线程,直到发生以下任何一种情况为止:
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
- 已调用Runtime类的退出方法并且安全
经理已允许进行退出操作。 - 不是守护程序线程的所有线程
都已死亡,方法是从
调用返回到run方法,或者引发传播
的异常
因此没有什么可以阻止JVM终止。
So there is nothing stopping the JVM from terminating.
问题是,为什么要使用没有GUI的 javax.swing.Timer
?您要达到什么目的?
The question is, why are you using a javax.swing.Timer
without a GUI? What are you trying to achieve?
已更新
如果不这样做想要使用GUI,您将需要使用 java.util.Timer
,例如...
If you don't want to use a GUI, you will need to use a java.util.Timer
, for example...
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 ...
Otherwise you'll need to supply some kind 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();
}
}
}
这篇关于这个计时器有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!