2014-01-17 166 views
0

我有一个JFrame.I无法将图像添加到它。我不知道如何给出正确的图像路径。无法将图像添加到JFrame?

String iPath = "RemoteItServer/Mobile.png"; 
       JLayeredPane layeredPane = new JLayeredPane(); 
       layeredPane.setBounds(0, 0, 315, 610); 

       JLabel mobileImageLabel = new JLabel(new ImageIcon(iPath)); 
       mobileImageLabel.setBounds(0, 0, 315, 610); 
       layeredPane.add(mobileImageLabel, Integer.valueOf(0)); 

enter image description here

如果iPath = C://Mobile.png如果我给图像Shown.But iPath = "RemoteItServer/Mobile.png"iPath = "/RemoteItServer/res/images/Mobile.png"。它没有显示图像。

所以帮我在正确的方向:)

感谢您的帮助......

回答

1

下面的路径应该工作:

String iPath = "res/images/Mobile.png"; 

编辑:

也许你忘了将窗格添加到框架。

getContentPane().add(layeredPane); 
0

什么可以帮助很多是将图像直接放在您正在处理的项目中。这样你只需要调用“Mobile.png”而不必担心路径。

1

路径可能会非常棘手。如果相对路径,如由Joschua提供的一个,不能正常工作,则可以拼凑的绝对路径,像这样:

字符串工作目录= System.getProperty(“user.dir来”);
String separator = System.getProperty(“file.separator”);

然后,只需串连到工作目录的相对路径:

工作目录+隔板+ “RES /图像/ Mobile.png”;

另一种方式做,这是使用File类,像这样:

档案文件=新的文件( “RES /图片/ Mobile.png”);
String path = file.getAbsolutePath();

希望这会有所帮助。