| |
14. 114. 1. 图形界面定时器 |
|
- javax.swing.Timer can only be used in Swing applications.
- javax.swing.Timer is a more appropriate choice over java.util.Timer for Swing applications.
- javax.swing.Timer handles thread sharing.
- You implement the java.awt.event.ActionListener interface and write your task code in its actionPerformed method.
- Ro cancel a task, you use the javax.swing.Timer class's stop method.
|
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class MainClass {
public static void main(String[] args) {
Timer timer = new Timer(1000, new MyTimerActionListener());
timer.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
timer.stop();
}
}
class MyTimerActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("asdf");
}
}
|
|
|