デスクトップ・アプリケーションのスプラッシュ画面として機能する JWindow を作成しました。私が抱えている問題は、ウィンドウが表示された後、意図した画像を表示する前に一時的に空白になることです。空白のウィンドウが 0.5 秒から 2 秒の間残ることがあります。ウィンドウが表示される前に、コンテンツを完全にレンダリングしたいと思います。
Java 1.6 を使用して MacOS を使用しています。
起動直後のウィンドウは次のとおりです。
わずか 0.5 秒後に画像が表示されます。画像はかなり小さいです (約 95kBytes JPG)。コンストラクターはイメージがロードされるまでブロックするはずなので、問題は ImageIcon のロードではないことはわかっています。
これが私のコードです:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
public class SplashScreen extends JWindow
{
public SplashScreen()
{
ClassLoader classLoader = this.getClass().getClassLoader();
ImageIcon imageIcon = new ImageIcon(classLoader.getResource("res/landscape.jpg"));
while (imageIcon.getImageLoadStatus() != MediaTracker.COMPLETE) {}
JLabel lbl = new JLabel(imageIcon);
getContentPane().add(lbl, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
MouseListener mouseListener = new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
setVisible(false);
dispose();
}
};
addMouseListener(mouseListener);
setVisible(true);
}
public static void main(String argv[])
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SplashScreen ss = new SplashScreen();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
EDIT : ループに imageIcon.getImageLoadStatus() を追加しましたが、効果はありません。