2014-01-17 4 views
0

나는 이미지를 추가 할 수 없습니다. 올바른 이미지 경로를 제공하는 방법을 모르겠습니다. 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)); 
내가 주면 이미지가 Shown.But되어 iPath = C://Mobile.png 경우

enter image description here

iPath = "RemoteItServer/Mobile.png" 또는 iPath = "/RemoteItServer/res/images/Mobile.png" .IT는 이미지를 표시하지 않습니다.

그래서

답변

1

다음 경로가 작동해야 올바른 방향 : 당신의 도움에 대한

감사합니다 ... 저를 도와 :

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

편집 :

그리고 어쩌면 당신을 프레임을 창에 추가하는 것을 잊었습니다.

getContentPane().add(layeredPane); 
0

작업중인 프로젝트에 이미지를 직접 배치하는 것이 많은 도움이 될 수있는 것은 무엇입니까? 이 방법을 사용하면 "Mobile.png"을 호출하고 경로를 염려 할 필요가 없습니다.

1

경로가 까다로울 수 있습니다. Joschua에서 제공 한 것과 같은 상대 경로가 작동하지 않는 경우 다음과 같은 절대 경로를 함께 조각 할 수 있습니다.

String workingDir = System.getProperty ("user.dir");
문자열 구분 기호 = System.getProperty ("file.separator");

이어서 바로 workingDir 상대 경로 연결할 :

workingDir + 분리기 + "입술/이미지/Mobile.png"을; 이 작업을 수행하는 또 다른 방법

때문에 같은 파일 클래스를 사용하는 것입니다 ("고해상도/이미지/Mobile.png")

파일 파일 = 새로운 파일;
String path = file.getAbsolutePath();

희망이 도움이됩니다.