定时器延迟不起作用?
Timer Delay Not Working?
我试图让这个程序每秒在控制台打印当前时间。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.util.Date;
public class E10U27 extends JFrame {
public static void main(String[] args){
// Prep the listener to respond
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println(now);
}
}
ActionListener listener = new TimerListener();
final int DELAY = 1000;
Timer t = new Timer(DELAY, listener);
t.start();
}
}
但是,它只打印一次 50(例如 2:52 50 次),依此类推。它确实正确地勾选了。如何正确地运行?我的代码有错误吗?
final int DELAY = 999999;
Timer timer = new Timer(DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Your Action
}
});
timer.start();
在此处参考文档Java Docs
我运行代码没有错误,但是它完成时没有显示分配给Timer的间隔中的日期,所以我做了一些修改
- 将DELAY改为1000(1秒),每秒打印一次当前时间
final int DELAY = 1000;
- 创建框架使程序保持运行,否则main方法将完成
(new E10U27()).setVisible(true);
这是经过修改的程序,它每秒打印一次当前时间:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.util.Date;
public class E10U27 extends JFrame {
public static void main(String[] args){
// Prep the listener to respond
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println(now);
}
}
ActionListener listener = new TimerListener();
final int DELAY = 1000; // Milliseconds between timer ticks
Timer t = new Timer(DELAY, listener);
t.start();
(new E10U27()).setVisible(true);
}
}
我试图让这个程序每秒在控制台打印当前时间。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.util.Date;
public class E10U27 extends JFrame {
public static void main(String[] args){
// Prep the listener to respond
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println(now);
}
}
ActionListener listener = new TimerListener();
final int DELAY = 1000;
Timer t = new Timer(DELAY, listener);
t.start();
}
}
但是,它只打印一次 50(例如 2:52 50 次),依此类推。它确实正确地勾选了。如何正确地运行?我的代码有错误吗?
final int DELAY = 999999;
Timer timer = new Timer(DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Your Action
}
});
timer.start();
在此处参考文档Java Docs
我运行代码没有错误,但是它完成时没有显示分配给Timer的间隔中的日期,所以我做了一些修改
- 将DELAY改为1000(1秒),每秒打印一次当前时间
final int DELAY = 1000;
- 创建框架使程序保持运行,否则main方法将完成
(new E10U27()).setVisible(true);
这是经过修改的程序,它每秒打印一次当前时间:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.util.Date;
public class E10U27 extends JFrame {
public static void main(String[] args){
// Prep the listener to respond
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println(now);
}
}
ActionListener listener = new TimerListener();
final int DELAY = 1000; // Milliseconds between timer ticks
Timer t = new Timer(DELAY, listener);
t.start();
(new E10U27()).setVisible(true);
}
}