Ram*_*ama 3 java swing timer actionlistener
我希望我的计时器在5秒的时间内只执行一次actionPerformed方法,但它在控制台"Hello"中写了很多次:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class X{
public static void main(String args[]) {
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println( "Hello" );
}
};
Timer timer = new Timer( 5000, actionListener );
timer.start();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能达到我想要的效果?谢谢
如前所述,最好使用java.util.Timer,但您也可以在开始之前使用setRepeats():
timer.setRepeats(false);
Run Code Online (Sandbox Code Playgroud)