【发布时间】:2014-07-01 15:59:21
【问题描述】:
好的,所以我有一个程序,您可以在其中上传出租房屋的图像,然后将图像复制到所有图像所在的目录。然后在另一个窗口中,它将所有图像加载到我可以单击的演示文稿中。在通过 Sublime Text 编译和运行时,它工作得非常好。但是在创建一个 .jar 文件并运行它之后,出现了一些问题。我可以上传图像并将其复制到图像目录中,但是当我转到应该显示所有图像的另一个 JFrame 窗口时,它会锁定程序并且必须通过任务管理器关闭。有什么建议?
这是启动程序时的代码,其中一些房屋的库存图像被加载到所有房屋图像的主图像目录中:
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 );
}
然后我有一个很长的 Image 类,我在 PasteBin 上发布了代码:
【问题讨论】:
-
更相关的是事件处理代码。如果操作不当,UI 线程可能会被锁定。