//主窗口的代码
package test;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
Timer m_tLzq=null; //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jButton1.setBounds(new Rectangle(81, 45, 73, 25));
jButton1.setText("Start");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton2.setBounds(new Rectangle(211, 46, 73, 25));
jButton2.setSelected(false);
jButton2.setText("Stop");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jTextField1.setText("jTextField1");
jTextField1.setBounds(new Rectangle(97, 110, 171, 30));
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jTextField1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e)
{
m_tLzq=new Timer(100,new Untitled1());
m_tLzq.setRepeats(true);
m_tLzq.start();
} void jButton2_actionPerformed(ActionEvent e)
{
m_tLzq.stop();
}
}class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}//Timer监听事件
package test;import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;public class Untitled1 implements ActionListener
{
private static int iCount=0;
public void actionPerformed(ActionEvent e)
{
try
{
System.out.println(iCount++);
}
catch(Exception s)
{
System.out.println(e.toString());
}
}
}
package test;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
Timer m_tLzq=null; //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jButton1.setBounds(new Rectangle(81, 45, 73, 25));
jButton1.setText("Start");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton2.setBounds(new Rectangle(211, 46, 73, 25));
jButton2.setSelected(false);
jButton2.setText("Stop");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jTextField1.setText("jTextField1");
jTextField1.setBounds(new Rectangle(97, 110, 171, 30));
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jTextField1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e)
{
m_tLzq=new Timer(100,new Untitled1());
m_tLzq.setRepeats(true);
m_tLzq.start();
} void jButton2_actionPerformed(ActionEvent e)
{
m_tLzq.stop();
}
}class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}//Timer监听事件
package test;import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;public class Untitled1 implements ActionListener
{
private static int iCount=0;
public void actionPerformed(ActionEvent e)
{
try
{
System.out.println(iCount++);
}
catch(Exception s)
{
System.out.println(e.toString());
}
}
}
就是用JBuilder生成一个Application.
在窗口上放两个按钮,一个让Timer开始,一个让Timer结束,
然后从ActionListener继承一个事件,加到Timer里.
在事件里输出一个不断增加的整数值.
可一把系统时间调小后,Timer就不输出了,调大没事.
高手快给看看啊!!
它是用System.currentTimeMillis()记录开始时间的
你时间改小了是不满足条件了
那么这种Timer有什么意义呢?
怎样才能避免这种情况呢?
taskTime = System.currentTimeMillis()+1000
有个线程在扫描任务,当 System.currentTimeMillis()==taskTime 就执行
改了时间后,System.currentTimeMillis()变小,只有等到满足条件时在执行了避免?谁没事改时间干吗啊,避免这个不就行了
这时候你告诉他:不行,我的程序正在运行,你不能改系统时间!
这好象说不过去啊?老大!如何才能设计一个与系统时间无关,一个健壮的Timer呢?
感觉java 很好呀
Timer t = new Timer(true);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE,min);
cal.set(Calendar.SECOND,sec);
Date d = new Date();
t.schedule(new TimerTask() { public void run() {
System.out.println("-----");
}
},cal.getTime());
System.in.read();
在等待的过程中把windows的时间后退一点,timer可以正常在指定的时间打印出语句
我的程序中有一个Timer,一秒钟Timer一次,import java.util.*;Timer t=new Timer(1000,new ActionListener()
{
public void actionPerformaed()
{
System.out.println("fu ck Java!!");
}
});
程序起动后,能一秒钟输出一个F uck Java
可是一把系统时间调小就不行了.调大可以.
如果系统时间调小了,的确会造成Timer停顿一个时间,但是并不会不再运行
其实是这样的,目前系统时间是9:30:00,这个时候刚好执行完println
Java就等待时间来到9:30:01秒时候开始工作,但是我改时间为9:25:00了,
Java还是会等待到9:30:01秒,这个时候Java还是会正常的走下去的
只是中间停顿了5分钟如果你对这样的结果不能接受你就自己写一个Thread就可以了,但是我觉得系统时间N个月都
不会调整一次,现在调整也只是造成了一些延时并没有失效,我觉得并没有问题
Timer t = new Timer(true);
t.schedule(new TimerTask() {
int i = 0;
public void run() {
System.out.println("-----" + i++);
}
},0,1000);
System.in.read();
哎!没想到Java里的定时器这么差劲!!
为了程序的健壮性,我必须要保证程序在系统时间不断被人调整的情况下正常工作,
谢谢大家!