import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JWindow; public class SplashDemo { private static final int SHOW_FOR = 3000; public SplashDemo() { JFrame frame = new JFrame("Splash-Demo"); new Splash("img/rot300x200.png", SHOW_FOR, frame); frame.setVisible(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 400); frame.setLocationRelativeTo(null); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new SplashDemo()); } } class Splash extends JWindow { private int min = 0, max = 100; private boolean show = true; final JProgressBar progressBar = new JProgressBar(min, max); public Splash(final String imgPath, final int showFor, final JFrame frame) { this.setBackground(Color.WHITE); // Mausevent setzt bei Klick die Hilfsvariable show auf false, beendet // damit den Splashscreen und öffnet das Hauptfenster, sofern es // geladen ist. this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { show = false; setVisible(false); dispose(); } }); // Splash-Hauptsteuerung final Timer timer = new Timer(showFor); Thread wRunner = new Thread() { public void run() { timer.start(); // Splash zeigen solange er nicht unterbrochen wird (show=false) // oder das Anzeigeintervall abgelaufen ist. while (show && timer.counter <= showFor) { Splash.this.setVisible(true); } // Splash beenden Splash.this.setVisible(false); Splash.this.dispose(); // Hauptfenster anzeigen frame.setVisible(true); } }; // Progressbar-Thread final Runnable pbRunner = new Runnable() { public void run() { System.out.println("running progress bar"); for (int i = min; i <= max; i++) { try { Thread.sleep(showFor / max); } catch (InterruptedException e) { } progressBar.setValue(i); } } }; // Splash-GUI JPanel contentPane = new JPanel(); this.setContentPane(contentPane); contentPane.setLayout(new BorderLayout()); ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(imgPath)); this.setSize(icon.getIconWidth(), icon.getIconHeight()); contentPane.add(new JLabel(icon, JLabel.CENTER), BorderLayout.CENTER); contentPane.add(progressBar, BorderLayout.SOUTH); this.setLocationRelativeTo(null); this.setVisible(true); new Thread(wRunner).start(); new Thread(pbRunner).start(); } } // class Splash class Timer extends Thread { int counter = 0, showFor; public Timer(int showFor) { this.showFor = showFor; } public void run() { System.out.println("timer running"); while (counter <= showFor) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } counter++; } } } // class Timer