我是新來的揮杆,我試圖顯示,我用下面的代碼拖進我的項目一個簡單的圖像。一切都編譯並運行,但是,圖像不顯示。爪哇 - 鞦韆顯示圖像不工作
順便說一下,我真的很喜歡這樣做,而不是從文件路徑中獲取圖像。
代碼:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ImageProcessorApp IPA = new ImageProcessorApp();
IPA.displayImage();
}
void displayImage() throws IOException {
JFrame frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
BufferedImage wPic = ImageIO.read(this.getClass().getResource("url-2.jpg"));
JLabel wIcon = new JLabel(new ImageIcon(wPic));
frame.add(wIcon);
System.out.println("added image");
}
沒有'this.getClass()的getResource( 「URL-2.JPG」)'返回一個非空的對象?我的猜測是文件名/路徑是錯誤的。你到底在哪條路徑上拖動圖像? – cello
是類路徑中的圖像文件,所以getResource方法可以找到它?上看到類路徑是:的System.out.println(System.getProperty( 「java.class.path」)); – NormR