• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JWindowを使ったSplash Screenの表示 [#u76ddf25]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2003-09-25~
更新日:&lastmod;
---
category: swing
folder: SplashScreen
title: JWindowを使ったSplash Screenの表示
tags: [JWindow, JLabel]
author: aterai
pubdate: 2003-10-13T06:25:21+09:00
description: JWindowを使って、Splash Screenを表示します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTtxuiuuI/AAAAAAAAAk4/JhuyuS80C4M/s800/SplashScreen.png
---
* 概要 [#summary]
`JWindow`を使って、`Splash Screen`を表示します。以下のサンプルコードは、`%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java`から引用改変したものです。

#contents
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTtxuiuuI/AAAAAAAAAk4/JhuyuS80C4M/s800/SplashScreen.png)

**概要 [#v9cffe59]
JWindowを使って、Splash Screenを表示します。起動画面、Splash Windowといった呼び方もあるようです。
以下のサンプルコードは、''%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java'' から引用改変したものです。
* サンプルコード [#sourcecode]
#code(link){{
private JWindow splashScreen;
private JLabel  splashLabel;
public MainPanel() {
  super();
  createSplashScreen(SPLASH_PATH);
  EventQueue.invokeLater(new Runnable() {
    @Override public void run() {
      showSplashScreen();
    }
  });
  // 実行時間の長い処理など
  try {
    Thread.sleep(3000);
  } catch (InterruptedException e) {
    System.out.println(e);
  }
  EventQueue.invokeLater(new Runnable() {
    @Override public void run() {
      showPanel();
      hideSplash();
    }
  });
}

//#screenshot
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);
}

**サンプルコード [#raad4e3d]
 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();
   //といった初期化などの重い処理の代わりに、このサンプルではsleep
   try{
     Thread.sleep(3000);
   }catch(InterruptedException e) {
   	 System.out.println(e);
   }
   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       showPanel();
       hideSplash();
     }
   });
 }
public void showSplashScreen() {
  splashScreen.setVisible(true);
}

-&jnlp;
-&jar;
-&zip;
public void hideSplash() {
  splashScreen.setVisible(false);
  splashScreen = null;
  splashLabel = null;
}

**解説 [#j7466f5a]
スプラッシュ・スクリーンのスレッド処理をSwingUtilities.invokeLaterメソッドを使って行っています。使用する画像は事前に用意しておいてください。上記のサンプルコードでは、クラスパスの通った場所にある ''/resources/images/splash.png'' を読み込んで表示するようになっています。
public JFrame getFrame() {
  return frame;
}

サンプルの画像は、[[n-Gen>http://www.n-generate.com/download.html]]を使って生成しています。
public void showPanel() {
  this.setPreferredSize(new Dimension(300, 200));
  frame.getContentPane().add(this);
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
}
}}

**コメント [#gc355cfa]
* 解説 [#explanation]
上記のサンプルコードでは、クラスパスの通った場所にある`/resources/images/splash.png`を読み込んでスプラッシュ・スクリーン(起動画面)を表示しています。`JWindow`の表示、非表示は、イベントディスパッチスレッド(`EDT`)で行われるように、`Runnable`を作成し`EventQueue.invokeLater()`メソッドで実行しています。

----
- `Java SE 6`では[https://docs.oracle.com/javase/jp/8/docs/api/java/awt/SplashScreen.html SplashScreen]クラスが追加され、起動時にスプラッシュ・スクリーンとして表示する画像をコマンドラインや`manifest.mf`で指定するなどの方法で使用可能になった
-- [https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html How to Create a Splash Screen (The Java™ Tutorials > Creating a GUI With Swing > Using Other Swing Features)]
-- [http://web.archive.org/web/20090419180550/http://java.sun.com/developer/JDCTechTips/2005/tt1115.html#1 SPLASH SCREENS AND JAVA SE 6]
-- [http://www.oracle.com/technetwork/articles/javase/splashscreen-135938.html New Splash-Screen Functionality in Java SE 6]

* 参考リンク [#reference]
- [[JDialogでモーダルなJProgressBar付きSplash Screenを表示する>Swing/ProgressSplashScreen]]

* コメント [#comment]
#comment
- `Splash Screen`を`Java SE 6`の機能で表示させるために`splashScreen.setVisible(false)`にしていたテスト版のサンプルが添付されていたのを修正。 -- &user(aterai); &new{2007-09-25 (火) 18:56:13};

#comment