import java.awt.event.*;
import javax.swing.*;
public class TestTimer implements ActionListener
{
Timer timer=new Timer(1000,this);
public TestTimer(){
timer.start();
try {
Thread.sleep(10000); // 让程序计时10s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
System.out.println("1");
}
public static void main(String[] args){
TestTimer t=new TestTimer();
}
}
//加个变量k让timer跑10次
import java.awt.event.*;
import javax.swing.*;
public class TestTimer implements ActionListener
{
Timer timer=new Timer(1000,this);
int k = 0;
public TestTimer(){
timer.start();
while(k<10);
}
public void actionPerformed(ActionEvent e){
System.out.println(k++);
}
public static void main(String[] args){
TestTimer t=new TestTimer();
}
}
public static void main(String[] args){
TestTimer t=new TestTimer();
t.actionPerformed(你要传的参数);
}