我正在制作一个应用程序的启动画面。这是一个JWindow
JPanel
和JLabel
我ImageIcon
上有一个JLabel
。使用ImageIcon
从InputStream
加载this.getClass.getResourceAsStream("GenericApp.png");
。我的启动画面代码如下:
final JWindow window = new JWindow();
JPanel jp = new JPanel();
InputStream is = this.getClass().getResourceAsStream("GenericApp.png");
Image image = null;
try {
image = ImageIO.read(is);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel l = new JLabel(new ImageIcon(image));
window.add(jp);
jp.add(l);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
window.setVisible(false);
当我启动我的项目时,我得到一个空白的JWindow,其中包含我设置的尺寸。
答案 0 :(得分:2)
以下是我解决它的方法:
显然,图像需要一段时间才能加载。我在加载图像之前调用了Thread.sleep(5000)
,从而切断了显示过程。故事的寓意是你几乎不想使用Thread.sleep()
使用定时器。
此致
托马斯