最近,在準備課題作業(yè),想通過sokect編程來實現(xiàn)窗體小程序,;實踐過程中才發(fā)現(xiàn)無限難,不過,,懷著拿學歷和學位證的信心和毅力去完成這么個小玩意,。 說正題,因為窗體默認是java圖標,,于是想實現(xiàn)自定義窗體的icon,,通過ImageIO去讀取文件的時候報錯。 控制臺報錯: javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at com.bomei.image.ImageUtils.pressImage(ImageUtils.java:41)
at com.bomei.image.ImageUtils.main(ImageUtils.java:117)
剛開始用ImageIO直接讀取File文件,。 JFrame f = new JFrame("WAR"); try { Image img = ImageIO.read(this.getClass().getResource("/img/cwm02.png"); f.setIconImage(img);//icon f.setBounds(300, 300, 360, 300); f.setVisible(true); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } catch (IOException e) { e.printStackTrace(); }
解決方法(很簡單用流解析圖片就行): //1.絕對路徑下指定并解析圖片通過FileInputStream讀取 String path = "E://Selenium/ChattingMe/src/main/com/tangsg/img/cwm02.png"; InputStream is=new FileInputStream(new File(path)); //2.項目工程根路徑下解析圖片通過FileInputStream讀取 String path = "src/main/com/tangsg/img/cwm02.png"; InputStream is=new FileInputStream(new File(path)); //3.項目工程相對路徑下解析圖片通過ClassLoader讀取 String path = "/com/tangsg/img/cwm02.png"; InputStream is = JFIconFir.class.getResourceAsStream(path); Image img = ImageIO.read(is);
|