import sun.misc.*; // Sun's Timer undocumented class
import java.util.*; // Calendar class public class TimerTest implements Timeable {
public static void main(String[] args) {
TimerTest me = new TimerTest();
Timer ticker = new Timer(me, 1000);
ticker.cont();
System.out.println("Timer started");
}
public void tick(Timer t) {
System.out.println
("\007Being ticked " + Calendar.getInstance().getTime());
}
}
The Timer class is now part of the Swing package. The Timer object will send an ActionEvent to the registered ActionListener. import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;public class TimerDemo implements ActionListener {
Timer t = new Timer(1000,this); TimerDemo() {
t.start();
} public static void main(String args[]) {
TimerDemo td = new TimerDemo();
// create a dummy frame to keep the JVM running
// (for demonstation purpose)
java.awt.Frame dummy = new java.awt.Frame();
dummy.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {
System.out.println
("\007Being ticked " + Calendar.getInstance().getTime());
}
}
}
import java.util.*; // Calendar class public class TimerTest implements Timeable {
public static void main(String[] args) {
TimerTest me = new TimerTest();
Timer ticker = new Timer(me, 1000);
ticker.cont();
System.out.println("Timer started");
}
public void tick(Timer t) {
System.out.println
("\007Being ticked " + Calendar.getInstance().getTime());
}
}
The Timer class is now part of the Swing package. The Timer object will send an ActionEvent to the registered ActionListener. import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;public class TimerDemo implements ActionListener {
Timer t = new Timer(1000,this); TimerDemo() {
t.start();
} public static void main(String args[]) {
TimerDemo td = new TimerDemo();
// create a dummy frame to keep the JVM running
// (for demonstation purpose)
java.awt.Frame dummy = new java.awt.Frame();
dummy.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {
System.out.println
("\007Being ticked " + Calendar.getInstance().getTime());
}
}
}
你可以看看JDK1.3以后的稳当