2014-10-02 59 views
0

我在我的程序下面的代码:图像不显示与setIcon()来

JLabel status = new JLabel(); 
status.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
status.setBounds(400, 30, 380, 200); 
status.setIcon(new ImageIcon("C:........./logo2.png")); 
status.setLocation(50, 750); 
add(status); 

但现在我想从setIcon(new ImageIcon("path"))删除对我有一个文件夹里在我的项目的图像的路径。

我试着这样做:

status.setIcon(new ImageIcon("src/folder/logo2.png")); 

但它并没有显示任何内容。

我发现this topic,并试图这也:

ImageIcon(this.getClass().getResource("src/files/logo2.png")); 

但它给了我一个空指针。

这是我的项目的结构:

enter image description here

我怎样才能正确显示此图像?

+0

请显示您的项目的文件夹结构。 – 2014-10-02 19:43:19

+0

好吧,我刚刚编辑我的问题 – Rikkin 2014-10-02 19:49:06

+2

你试过'ImageIcon(this.getClass()。getResource(“/ files/logo2.png”));'? – 2014-10-02 19:53:46

回答

0

看你必须在Project文件夹下创建一个新的文件夹,调用它的资源或任何你想要的东西。之后,您可以参考您的资源getClass().getResource("resources/thefile.png").toFile() 希望可以帮助