• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JWindowを使ったSplash Screenの表示
#navi(../)
RIGHT:Posted by [[terai]] at 2003-09-25
RIGHT:Posted by [[aterai]] at 2003-09-25
*JWindowを使ったSplash Screenの表示 [#u76ddf25]
JWindowを使って、Splash Screenを表示します。起動画面、Splash Windowといった呼び方もあるようです。以下のサンプルコードは、''%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java'' から引用改変したものです。

-&jnlp;
-&jar;
-&zip;

#screenshot
//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTTtxuiuuI/AAAAAAAAAk4/JhuyuS80C4M/s800/SplashScreen.png)

**サンプルコード [#raad4e3d]
#code{{
private JWindow splashScreen;
private JLabel  splashLabel;
public MainPanel() {
  super();
  createSplashScreen(SPLASH_PATH);
  EventQueue.invokeLater(new Runnable() {
    public void run() {
      showSplashScreen();
    }
  });
  //長い処理のdummy
  try{
    Thread.sleep(3000);
  }catch(InterruptedException e) {
    System.out.println(e);
  }
  EventQueue.invokeLater(new Runnable() {
    public void run() {
      showPanel();
      hideSplash();
    }
  });
}
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.setVisible(true);
}
public void hideSplash() {
  splashScreen.setVisible(false);
  splashScreen = null;
  splashLabel  = null;
}
public JFrame getFrame() {
  return frame;
}
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);
}
}}

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

サンプルの画像は、[[n-Gen>http://www.n-generate.com/download.html]]を使って生成しています。
サンプルの画像は、[http://www.n-generate.com/download.html n-Gen]を使って生成しています。

----
Java SE 6 では、起動時にスプラッシュ・スクリーンとして表示する画像をコマンドラインで、またはmanifest.mfで指定したり、SplashScreenオブジェクトを生成してこれを表示したりすることができるようです。
-[[SPLASH SCREENS AND JAVA SE 6>http://java.sun.com/developer/JDCTechTips/2005/tt1115.html#1]]
-[[New Splash-Screen Functionality in Java SE 6>http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/]]
-[http://java.sun.com/developer/JDCTechTips/2005/tt1115.html#1 SPLASH SCREENS AND JAVA SE 6]
-[http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/ New Splash-Screen Functionality in Java SE 6]

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

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

#comment