Eclipse Java应用程序可以';找不到资源
我有一个我正在制作的应用程序的启动屏幕。它是一个Eclipse Java应用程序可以';找不到资源,java,swing,embedded-resource,Java,Swing,Embedded Resource,我有一个我正在制作的应用程序的启动屏幕。它是一个JWindow,带有JPanel和JLabel的窗口,然后在JLabel上有一个ImageIcon。使用this.getClass.getResourceAsStream(“GenericApp.png”),ImageIcon从InputStream加载。我的启动屏幕代码如下: final JWindow window = new JWindow(); JPanel jp = new JPanel(); InputStream is = this.
JWindow
,带有JPanel
和JLabel
的窗口,然后在JLabel
上有一个ImageIcon
。使用this.getClass.getResourceAsStream(“GenericApp.png”),ImageIcon
从InputStream
加载代码>。我的启动屏幕代码如下:
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);
当我启动我的项目时,我得到一个带有我设置的维度的空白窗口。以下是我如何解决它的:
显然,加载图像需要一段时间。我正在调用Thread.sleep(5000)
,它还没加载完图像,就中断了显示过程。这个故事的寓意是,你几乎永远不想使用线程。sleep()
而是使用计时器
问候,
Thomas那么,this.getClass()返回的类的包是什么?您把png文件放在哪里了?有例外吗?堆栈跟踪是什么?没有堆栈跟踪,因为IconImage没有给出堆栈跟踪。this.getClass
的输出是:class iamthethomas.artint.CreateJFrame
png文件与CreateJFrame在同一个包中。1)不要阻止EDT(事件调度线程)。发生这种情况时,GUI将“冻结”。有关详细信息和修复方法,请参阅。2) window.setBounds(500150300200)代码>最好是window.pack();/*根据图像调整窗口大小*/window.setLocation(500150)
@thewrenchinsystem:Don'tsleep()
在EDT上;一定要看和看。