好的,所以我有一個程序可以上傳房屋的圖像以供租借,然後將圖像複製到所有圖像所在的目錄中。然後在另一個窗口中加載所有可以點擊的圖像。這在編譯和通過Sublime Text運行時工作得很好。但是在創建一個.jar文件並運行後,會出現一些問題。我可以上傳一個圖像並將其複製到圖像目錄中,但是當我到另一個應該呈現所有圖像的JFrame窗口時,它會鎖定程序並且必須通過任務管理器關閉。有什麼建議麼?在.jar文件中讀取圖像時程序崩潰
public void copyStockImages(String filename)
{
String filepath = "Images/Testimages/" + filename;
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getResource(filepath));
}
catch(FileNotFoundException fnfe){
System.out.println("File not found: " + filepath);
}
catch(IOException ioe){
System.out.println("Error reading image");
}
String newFilePath = "Images/houseImages/" + filename;
File fileOut = new File(newFilePath);
if(fileOut.exists()){
return;
}
try{
ImageIO.write(image, "jpg", fileOut);
}
catch(IOException ioe){
System.out.println("Error reading image");
}
}
上傳圖片:
private void uploadImages(HouseForRent house) {
if(filepaths.isEmpty()) {
return;
}
Iterator<String> iter = filepaths.iterator();
while(iter.hasNext()) {
String filepath = i.next();
String filetype = findFiletype(filepath);
BufferedImage image = null;
try {
image = ImageIO.read(new File(filepath));
}
catch(FileNotFoundException fnfe) {
dialog("Cannon find file: " + filepath);
}
catch(IOException ioe) {
dialog("Error reading file");
}
catch(NullPointerException npe) {
}
try {
saveImage(image, filetype, house);
}
catch(IOException ioe) {
dialog("Error reading from file: " + ioe.toString());
}
catch(NullPointerException npe) {
dialog("Error: " + npe.toString());
}
}
}
複製
啓動程序,其中庫存圖片一些房屋的被裝載到主圖像目錄房屋的所有圖像時,下面的代碼圖像轉到目錄:
private void saveImage(BufferedImage image, String filetype, HouseForRent house) throws IOException{
String newFilePath = "Images/houseImages/";
String filelink = newFilepath + filetype;
File fileOut = new File(filelink);
if(fileOut.exists()) {
house.addImage(filelink);
return;
}
try {
ImageIO.write(image, "jpg", fileOut);
}
catch(Exception e) {
dialog("Error reading image");
}
house.addImage(filelink);
imageLinks.add(filelink);
}
然後我有這個長的圖像類我發佈的代碼在引擎收錄:
更相關的將是事件處理代碼。如果你做得不當,UI線程可能被鎖定。 –