Java JLabel.setIcon()无法按预期工作
我正在为JavaSwing编写这个GoogleMap组件。我只需要使用谷歌静态地图。我希望地图根据读取用户输入的按钮操作进行更新,但包装在JLabel中的图像不会更新 我将静态映射缓存在mapCache.jpg中,并在每次启动ActionListener时将JLabelmapContent图标设置为静态映射。但它就是不起作用。mapCache.jpg在我的系统上更新,但在程序中不更新。我试图从JScrollPane中删除imageicon或mapContent,但这两种方法都不起作用。我怀疑JVM缓存了映像文件。如果是,如何清除缓存 以下是我的课堂内容:Java JLabel.setIcon()无法按预期工作,java,swing,jlabel,imageicon,Java,Swing,Jlabel,Imageicon,我正在为JavaSwing编写这个GoogleMap组件。我只需要使用谷歌静态地图。我希望地图根据读取用户输入的按钮操作进行更新,但包装在JLabel中的图像不会更新 我将静态映射缓存在mapCache.jpg中,并在每次启动ActionListener时将JLabelmapContent图标设置为静态映射。但它就是不起作用。mapCache.jpg在我的系统上更新,但在程序中不更新。我试图从JScrollPane中删除imageicon或mapContent,但这两种方法都不起作用。我怀疑JV
private static final long serialVersionUID = 2243575060653389810L;
private String latlong;
private int zoomLevel;
private JLabel mapContent;
private ImageIcon mapImage;
public MapComponent(float lat, float lon, int zoom){
latlong = validateLatlong(lat,lon);
zoomLevel = validateZoomLevel(zoom);
queryMap();
setLayout(new BorderLayout());
mapImage = new ImageIcon("mapCache.jpg");
mapContent = new JLabel(mapImage);
JScrollPane sp = new JScrollPane(mapContent);
add(sp, BorderLayout.CENTER);
JPanel inputPane = new JPanel();
JLabel latLabel = new JLabel("Latitude: ");
final JTextField latText = new JTextField(""+lat);
JLabel longLabel = new JLabel("Longitude: ");
final JTextField longText = new JTextField(""+lon);
JLabel zoomLabel = new JLabel("Zoom Level: ");
final JTextField zoomText = new JTextField(""+zoomLevel);
zoomText.setPreferredSize(new Dimension(20,15));
JButton mapGo = new JButton("Go");
mapGo.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try{
queryMap(Float.parseFloat(latText.getText()), Float.parseFloat(longText.getText()), Integer.parseInt(zoomText.getText()));
mapImage.setImage(new ImageIcon("mapCache.jpg").getImage());
mapContent.setIcon(null);
mapContent.setIcon(mapImage); // Doesn't work!
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
inputPane.add(latLabel);
inputPane.add(latText);
inputPane.add(longLabel);
inputPane.add(longText);
inputPane.add(zoomLabel);
inputPane.add(zoomText);
inputPane.add(mapGo);
add(inputPane, BorderLayout.PAGE_END);
setVisible(true);
}
private void queryMap(){
try {
String imageUrl = "http://maps.google.com/staticmap?center="+latlong+"&zoom="+zoomLevel+
"&size=1000x750&maptype=roadmap&markers="+latlong+
"&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg";
System.out.println(imageUrl);
String destinationFile = "mapCache.jpg";
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void queryMap(float lat, float lon, int zoom){
latlong = validateLatlong(lat,lon);
zoomLevel = validateZoomLevel(zoom);
queryMap();
}
private String validateLatlong(float lat, float lon){
lat = Math.min(lat, 90);
lat = Math.max(lat, -90);
lon = Math.min(lon, 90);
lon = Math.max(lon, -90);
return lat+","+lon;
}
private int validateZoomLevel(int zoom){
zoom = Math.min(zoom, 15);
zoom = Math.max(zoom, 1);
return zoom;
}
ImageIcon
,使用图像
作为其备份源,图像
将其图像数据缓存在内存中。在尝试将图像数据重新应用到标签之前,您需要刷新图像数据
mapContent.setIcon(null);
mapImage.getImage().flush();
mapContent.setIcon(mapImage); // Doesn't work! - Does now :)
我还建议您更好地查看和处理您的资源,例如
try (InputStream is = url.openStream(); OutputStream os = new FileOutputStream(destinationFile)) {
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
}
ImageIcon
,使用图像
作为其备份源,图像
将其图像数据缓存在内存中。在尝试将图像数据重新应用到标签之前,您需要刷新图像数据
mapContent.setIcon(null);
mapImage.getImage().flush();
mapContent.setIcon(mapImage); // Doesn't work! - Does now :)
我还建议您更好地查看和处理您的资源,例如
try (InputStream is = url.openStream(); OutputStream os = new FileOutputStream(destinationFile)) {
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
}
图像已缓存。您需要使用以下方法刷新图像:
icon = new ImageIcon(...);
icon.getImage().flush();
label.setIcon( icon );
或者,您可以仅使用ImageI/O读取图像:
label.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );
图像已缓存。您需要使用以下方法刷新图像:
icon = new ImageIcon(...);
icon.getImage().flush();
label.setIcon( icon );
或者,您可以仅使用ImageI/O读取图像:
label.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );
@当我在设置图标之前刷新时,我的图标会完全消失。有什么想法吗?@我唯一能想到的是图标不再存在(或者仍然被重新加载)@MadProgrammer当我在setIcon之前刷新时,我的图标完全消失了。有什么想法吗?@我唯一能想到的是图标不再存在(或者仍然被重新加载)