Using a Timer in Swing to display a picture for 5 seconds

后端 未结 2 696
小鲜肉
小鲜肉 2021-01-28 23:37

I am trying to make a login picture for my application using Timer. The idea is, when the user opens the application, he will see a picture for 5 seconds, then the

2条回答
  •  情歌与酒
    2021-01-29 00:10

    Start by creating the Timer once in the constructor. The Timer should, also, only make the CURRENT instance of login close

    public login() {
        //...
        time = new Timer(5000,new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                  dispose();
             }
         });
         timer.setRepeats(false);
    }
    

    In the shoutoff method, you start timer...

    public void shoutoff(){
        if (!time.isRunning()) {
            timer.start();
        }
    }
    

    Take a closer look at How to use Swing Timers for more details.

    You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

提交回复
热议问题