Swing/SplashScreen のバックアップの現在との差分(No.5)
2024-02-02 (金) 11:52:27
概要
JWindow
を使って、Splash Screen
を表示します。以下のサンプルコードは、%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java
から引用改変したものです。
Screenshot
Advertisement
private JWindow splashScreen; private JLabel splashLabel; public void createSplashScreen(String path){ ImageIcon img = new ImageIcon(getClass().getResource(path)); splashLabel = new JLabel(img); splashScreen = new JWindow(getFrame()); splashScreen.getContentPane().add(splashLabel); splashScreen.pack(); splashScreen.setLocationRelativeTo(null); } public void showSplashScreen(){ splashScreen.show(); } public void hideSplash(){ splashScreen.setVisible(false); splashScreen = null; splashLabel = null; } public HogeHoge(){ createSplashScreen("/resources/images/splash.png"); SwingUtilities.invokeLater(new Runnable(){ public void run(){ showSplashScreen(); } }); //initializePanel(); try{ Thread.sleep(3000); }catch(InterruptedException e){ System.out.println(e); } SwingUtilities.invokeLater(new Runnable(){ public void run(){ showPanel(); hideSplash(); } }); }
サンプルコード
解説
上記のサンプルコードでは、クラスパスの通った場所にある/resources/images/splash.png
を読み込んでスプラッシュ・スクリーン(起動画面)を表示しています。JWindow
の表示、非表示は、イベントディスパッチスレッド(EDT
)で行われるように、Runnable
を作成しEventQueue.invokeLater()
メソッドで実行しています。
- -
-
Java SE 6
ではSplashScreenクラスが追加され、起動時にスプラッシュ・スクリーンとして表示する画像をコマンドラインやmanifest.mf
で指定するなどの方法で使用可能になった