以下是主要代码如下privatevoidsetMapButtonActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:intshowConfirmDialog=JOptionPane.showConfirmDialog(M...
以下是主要代码如下
private void setMapButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int showConfirmDialog = JOptionPane.showConfirmDialog(MapPanel.this, "确定要更换地图?", "友情提示", JOptionPane.YES_NO_OPTION);
if (showConfirmDialog == 0) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String fileName = file.getName().toUpperCase();
if (fileName.endsWith(".JPG") || fileName.endsWith(".JPEG") || fileName.endsWith(".GIF")) {
return true;
} else {
return false;
}
}
}
@Override
public String getDescription() {
return "地图格式(.JPG;.JPEG;.GIF)";
}
});
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//String mapPath = this.getClass().getResource("img/map").getPath();
String mapPath = MapProcessor.class.getResource("/img/map").getPath();
new File(mapPath + "/" + dao.getMapName()).delete();
File selectedMapFile = fileChooser.getSelectedFile();
File upLoadFile = new File(mapPath + "/map" + selectedMapFile.getName().substring(selectedMapFile.getName().lastIndexOf(".")));
dao.setMapName(upLoadFile.getName());
try {
InputStream inStream = new FileInputStream(selectedMapFile);
OutputStream outStream = new FileOutputStream(upLoadFile);
int readBytes = 0; // 读取字节数
byte[] buffer = new byte[1024]; // 定义缓存数组
while ((readBytes = inStream.read(buffer, 0, 1024)) != -1) {// 从输入流读取数据到缓存数组中
outStream.write(buffer, 0, readBytes); // 将缓存数组中的数据输出到输出流
}
outStream.close();// 关闭输出流对象
inStream.close();// 关闭输入流对象
} catch (Exception ex) {
Logger.getLogger(MapPanel.class.getName()).log(Level.SEVERE, null, ex);
}
MapPanel.this.mapProcessor.replaceMap("/img/map/" + upLoadFile.getName());
slider.setValue(slider.getValue() + 1);
smallMapLabel.refreshScale();
smallMapLabel.setIcon(new ImageIcon(mapProcessor.zoom(200, 200)));
}
}
}
以下是在控制台出现的异常
D:\>java -jar MapModule.jar
五月 02, 2012 5:07:50 下午 com.mwq.map.MapPanel setMapButtonActionPerformed
严重: null
java.io.FileNotFoundException: file:\D:\MapModule.jar!\img\map\map.jpg (文件名、
目录名或卷标语法不正确。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at com.mwq.map.MapPanel.setMapButtonActionPerformed(MapPanel.java:387)
at com.mwq.map.MapPanel.access$20(MapPanel.java:352)
at com.mwq.map.MapPanel$21.actionPerformed(MapPanel.java:322)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
展开