javax.swing.Timer 的用法例程
来源:互联网 发布:qq留言软件 编辑:程序博客网 时间:2024/06/04 00:21
写某些程序(比如俄罗斯方块)时需要一个定时触发的事件去触发一段程序的执行,此时就要用到Timer。
javax.swing.Timer的官方文档是这样解释的
public class Timerextends Objectimplements Serializable
Fires one or more
ActionEvent
s at specified intervals. An example use is an animation object that uses aTimer
as the trigger for drawing its frames.Setting up a timer involves creating a Timer
object, registering one or more action listeners on it, and starting the timer using thestart
method. For example, the following code creates and starts a timer that fires an action event once per second (as specified by the first argument to theTimer
constructor). The second argument to the Timer
constructor specifies a listener to receive the timer's action events.
int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { //...Perform a task... } }; new Timer(delay, taskPerformer).start();
现将一个Timer的例程记录下来,仅供参考。
代码:
package test;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.Timer;public class Test extends JFrame implements ActionListener {private int now_s=0; public Test(){ new Timer(1000,this).start(); while(true); }@Overridepublic void actionPerformed(ActionEvent e) { System.out.println("now_s="+now_s); now_s++;}public static void main(String[] args){new Test();}}
运行效果:
(-------完--------)
0 0
- javax.swing.Timer 的用法例程
- javax.swing.Timer 的用法例程
- javax.swing.Timer的使用
- javax.swing.Timer 使用
- javax.swing.Timer类使用时遇到的问题。
- 使用javax.swing.Timer遇到的问题(已解决)。
- java 中javax.swing.timer类的初步使用
- javax.swing.Popup的应用
- Javax swing 包的 对话框
- Swing Timer 的使用案例
- Swing Timer 的使用案例
- java.java.util.Timer与javax.swing.Timer类重复定义
- java.util.Timer和java.util.TimerTask以及 javax.swing.Timer
- javax.swing
- javax.Swing
- javax.swing
- javax.swing
- import javax.swing.JOptionPane的问题
- hdu 1052 horse racing(贪心算法)
- 黑马程序员_java面向对象总结【1】(面向对象概述)
- Swing编程初步
- php引用传值 变量函数对象的引用
- Unity4.0 以上支持动态字体的研究
- javax.swing.Timer 的用法例程
- 通过action 使用 request.getParameter
- Java基础12--包--多线程
- 关闭浏览器的监听
- AS3使用PrintJob实现单页或多页打印
- MIPI 接口知识点
- 批量rebuild索引
- Blocks: Conclusion
- pku openjudge 拓扑排序