import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test extends JFrame implements ActionListener
{
Timer t;
JTextField t1;
int n=100;
public test()
{
t1=new JTextField(20);
t=new Timer(1000,this);
t.addActionListener(this);
this.add(t1);
this.setSize(200,200);
this.setVisible(true);
t.start();
}
public void actionPerformed(ActionEvent e)
{
if(n>0)
{
t1.setText(""+n);
n=n-1;
}
else
{
t.stop();
}
}
public static void main(String[] args)
{
new test();
}
}
输出结果为:99,97,95.。为何不是99,98,97,。。
import javax.swing.*;
import java.awt.event.*;
public class test extends JFrame implements ActionListener
{
Timer t;
JTextField t1;
int n=100;
public test()
{
t1=new JTextField(20);
t=new Timer(1000,this);
t.addActionListener(this);
this.add(t1);
this.setSize(200,200);
this.setVisible(true);
t.start();
}
public void actionPerformed(ActionEvent e)
{
if(n>0)
{
t1.setText(""+n);
n=n-1;
}
else
{
t.stop();
}
}
public static void main(String[] args)
{
new test();
}
}
输出结果为:99,97,95.。为何不是99,98,97,。。
//t.addActionListener(this);//这个注释就可以了
就OK了!
t.start();又向JTextField的事件发送了一次操作事件。