Java - Error on use of Timer

2019-03-02 05:31发布

问题:

So I have googled some minutes how to use the timer and found some helpfull threads here. But when I want to use the suggested code Eclipse shows me always an error.

int delay = 1000; //milliseconds
               ActionListener taskPerformer = new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                       label.setVisible(false);
                   }
               };
               new Timer(delay, taskPerformer).start();

This is my code I want to use now. Eclipse underlines the last line and when they suggest to "remove arguments to match 'Timer()' ". Further it underlines start() and want it to be casted. :S

Can someone help me pls? I've installed the last Java version^^

Thx a lot.

回答1:

take a look at your imports. Eclipse has auto imported java.util.Timer. Change that to javax.swing.Timer and you should be on your way.



回答2:

Import javax.swing.Timer not java.util.Timer.

Swing Timer has a constructor Timer(int , java.awt.event.ActionListener).



回答3:

your import is wrong there correct that

change

java.util.Timer

to

javax.swing.Timer