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,。。

解决方案 »

  1.   


    //t.addActionListener(this);//这个注释就可以了
      

  2.   

    注释掉t.addActionListener(this);
    就OK了!
      

  3.   

    t.addActionListener(this);向JTextField的事件发送了一次操作事件。
    t.start();又向JTextField的事件发送了一次操作事件。
      

  4.   

    actionPerformed调用了2次,注释掉一个就行了
      

  5.   

    在创建Timer时已经指定了监听new Timer(1000,this),在后面又指定监听t.addActionListener(this),调用了两次事件处理。注释掉t.addActionListener(this);