简体   繁体   中英

How to simultaneously display a splash screen and then my JFrame?

I am working on a splash screen. I managed to make a class. Below is a class that displays a splash screen. My problem is If I call this class from a JFrame and run, both JFrame and Splash screen runs at the same time and after the duration the splash screen is supposed to last Both of them is closed. How to I make them display simultaneously ?

Thanks a bunch

public class Splash extends JWindow {

AbsoluteLayout abs;
AbsoluteConstraints absImage, absBar;
ImageIcon image;
JLabel label;
JProgressBar bar;

public Splash() {
    abs = new AbsoluteLayout();
    absImage = new AbsoluteConstraints(0, 0);
    absBar = new AbsoluteConstraints(0, 210);
    label = new JLabel();
    image = new ImageIcon(this.getClass().getResource("anime.gif"));
    label.setIcon(image);
    bar = new JProgressBar();
    bar.setPreferredSize(new Dimension(350,10));
    this.getContentPane().setLayout(abs);
    this.getContentPane().add(label, absImage);
    this.getContentPane().add(bar, absBar);

    new Thread() {

        public void run() {
            for (int i = 0; i < 100; i++) {
                bar.setValue(i);
                try {
                    sleep(80);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Splash.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.exit(0);
        }
    }.start();
    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);

}

}

What effect do you think that

System.exit(0);

will have on your program? This is a sledgehammer way to close a window since it will cause the JVM to exit, closing any and all things that it was running.

Have you looked at using the SplashScreen object that Java Swing provides?

window.dipose();

dipose()将关闭窗口。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM