程序更改如下即可:import javax.swing.Timer;
import java.awt.event.*;public class TimeTest
{
javax.swing.JTextField t = new javax.swing.JTextField();
int delay = 500;
int count = 0;
Timer ti = new Timer(delay, new AL()); public static void main(String[] args)
{
TimeTest test = new TimeTest();
} TimeTest()
{
ti.start();
} class AL
implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
count++;
if (count == 0)
{
System.out.println("This is ocurred when count=0");
}
if (count == 10)
{
ti.stop();
count = -1;
}
System.out.println("" + count);
}
}
}
import java.awt.event.*;public class TimeTest
{
javax.swing.JTextField t = new javax.swing.JTextField();
int delay = 500;
int count = 0;
Timer ti = new Timer(delay, new AL()); public static void main(String[] args)
{
TimeTest test = new TimeTest();
} TimeTest()
{
ti.start();
} class AL
implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
count++;
if (count == 0)
{
System.out.println("This is ocurred when count=0");
}
if (count == 10)
{
ti.stop();
count = -1;
}
System.out.println("" + count);
}
}
}
神奇!但JTextField 实例t根本就没用呀!这对结果为什么会有影响呢?请解释!谢谢!
当你没有任何输入输出UI组件时,Timer类不会起任何作用。
如果你换成java.util.Timer类,就不会出现这种情况了。
这个事实,也是我反复实验您的代码,才发现的:)。