I used the java 7 class documentation exactly as it is written here . Am I doing something wrong? I am trying to write a timer to count down from 10 to 0 and display the timer numbers as it counts down. My intelliJ tells me that when I create a new Timer class and pass in the constructors for delay and taskPreformer that
Timer java.lang.String, boolean in timer cannot be applied to int java.awt.event.ActiveListener.
int delay = 1000; // milliseconds
ActionListener taskPerformer = new ActionListener() {
int count = 10;
public void actionPerformed(ActionEvent evt) {
if (count > 0) {
count--;
}
}
};
new Timer(delay, taskPerformer).start();
You have imported the wrong Timer
. There's a java.util.Timer
with arguments (String, boolean)
which is not the Timer
you want. The timer you want is the javax.swing.Timer
, which does have the arguments (int, ActionListener)
. So fix the imports
//import java.util.Timer; <-- take out
import javax.swing.Timer;
Also, you may want some stopping condition for the timer to stop. Maybe something like
if (count > 0) {
count--;
} else {
((Timer)evt.getSource()).stop();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.