我有一个程序在输入数据后显示静态Google地图,但是当我想刷新我的地图时,JLabel不会改变。这是我的按钮代码,只是清除地图:
JButton btnMakeMap = new JButton("Make map");
btnMakeMap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Map map = new Map(txfPlace.getText(), cmbMarkerName.getSelectedItem().toString().charAt(0), cmbMrkColour.getSelectedItem().toString(), Integer.parseInt(cmbZoom.getSelectedItem().toString()),
cmbMrkSize.getSelectedItem().toString(), chbCenter.isSelected(), cmbType.getSelectedItem().toString());
if(chbCenter.isSelected()){
map.genMarker();
map.genURlWcenter();
}else{
map.genMarker();
map.genURl();
}
lblMap.setIcon(null);
map.genimage();
ImageIcon im = new ImageIcon("map.jpg");
lblMap.setIcon(im);
}
});
btnMakeMap.setBounds(235, 146, 117, 49);
pnlSettings.add(btnMakeMap);
JButton btnRefresh = new JButton("clear map");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Map map = new Map(txfPlace.getText(), cmbMarkerName.getSelectedItem().toString().charAt(0), cmbMrkColour.getSelectedItem().toString(), Integer.parseInt(cmbZoom.getSelectedItem().toString()),
// cmbMrkSize.getSelectedItem().toString(), chbCenter.isSelected(), cmbType.getSelectedItem().toString());
lblMap.setIcon(null);
// map.genimage();
// ImageIcon im = new ImageIcon("map.gif");
// lblMap.setIcon(im);
//
}
});
btnRefresh.setBounds(406, 172, 89, 23);
pnlSettings.add(btnRefresh);
这是它使用的类的代码:
package com.mymaps;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import org.apache.http.HttpConnection;
import org.jboss.remoting.transport.Connector;
public class Map {
private String URl = "https://maps.googleapis.com/maps/api/staticmap?";
private int Zoom = 1;
private String Location = "";
private String markColor = "";
private String Marker = "";
private char markName;
private String markSize = "";
private String mapSize = "&size=400x400";
private boolean wantMarkeratCenter = false;
private String mapType = "maptype=";
public Map(String location, char name, String colour, int zoom,
String mrksize, boolean center, String type) {
Location = location;
markName = name;
markColor = markColor + colour;
markSize = mrksize;
Zoom = zoom;
wantMarkeratCenter = center;
mapType = mapType + type;
fixLocation();
// if (wantMarkeratCenter){//check if the user wants a marker at the
// center
// genMarker();
// genURlWcenter();
// }else{
// if(!wantMarkeratCenter){
// genMarker();
// genURl();
// }
// }
}
public boolean validateURl() {// validates URl length
boolean isValid = false;
if (URl.length() < 0 || URl.length() > 2048) {
isValid = true;
} else {
isValid = false;// y? just because
}
return isValid;
}
public void genURl() {// creates the URl for use
if (wantsSize()) {
URl = URl + mapSize + "&zoom=" + Zoom + "&" + mapType + "&"
+ Marker;
} else {
URl = URl + "&" + mapType + Marker;
}
}
public void genURlWcenter() {// generates URl with the center on the
// location point
String loc = Location.replace('+', ',');
if (wantsSize()) {
URl = URl + "center=" + loc + mapSize + "&" + mapType + "&" + Marker;
} else {
URl = URl;
}
}
public void genMarker() {// generates the marker
String name = "" + markName;
Marker = "markers=color:" + markColor + "|label:" + name.toUpperCase()
+ "|" + Location;
}
private boolean wantsSize() {// checks if the user wanted a size or not
// based on the var size
boolean wantSize = false;
if (mapSize.length() != 0) {
wantSize = true;
}
return wantSize;
}
public String getMarker() {
return Marker;
}
public String getURl() {
return URl;
}
public void genimage() {
URL url;
try {
url = new URL(URl);
System.out.println(url);
System.out.println();
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("map.jpg");
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void fixLocation() {
Scanner sc = new Scanner(Location);
String temp = "";
while (sc.hasNext()) {
temp = temp + sc.next() + "+";
}
temp = temp.substring(0, temp.length() - 1);
Location = temp;
sc.close();
}
public String getLocation() {
return Location;
}
public ImageIcon getImage() {
ImageIcon im = new ImageIcon("map..jpg");
return im;
}
}
这是代码here
的可运行版本答案 0 :(得分:1)
lblMap.setIcon(null);
map.genimage();
ImageIcon im = new ImageIcon("map.jpg");
lblMap.setIcon(im);
看起来您正在创建map.jpg并在想要刷新时将其绑定到标签。使用此代码,刷新后仍然可以看到旧图像吗?你确认map.jpg是最新的吗?如果是,则可能是操作系统分页问题,而且当JVM要求时,操作系统仍会返回旧文件。您可以通过每次刷新
生成唯一文件名来解决此问题