问题
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);
}
}
回答1:
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?
回答2:
window.dipose();
The dipose() will close the window.
来源:https://stackoverflow.com/questions/10668308/how-to-simultaneously-display-a-splash-screen-and-then-my-jframe