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

后端 未结 2 697
小鲜肉
小鲜肉 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:08

    The idea is, when the user opens the application, he will see a picture for 5 seconds, then the application will start.

    You should use a Splash Screen. The advantage of the splash screen is the image is displayed immediately as the whole Swing app doesn't need to be loaded.

    Check out the section from the Swing tutorial on How to Create a Splash Screen for more information and a working example.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题