2015-08-28 58 views
0

我有一個程序在輸入數據後顯示靜態Google地圖,但是當我想刷新我的地圖時,JLabel不會更改。這裏是我的代碼的按鈕一個剛剛清除地圖:如何讓我的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

我看不到標籤初始化或添加到組件的位置。考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的混淆和更好的反應 – MadProgrammer

回答

1
  lblMap.setIcon(null); 
      map.genimage(); 
      ImageIcon im = new ImageIcon("map.jpg"); 
      lblMap.setIcon(im); 
的可運行版本

看起來您正在創建map.jpg並將其綁定到標籤上以便刷新。有了這段代碼,刷新後你還能看到舊圖像嗎?你是否證實map.jpg是最新的?如果是的話,那麼當JVM詢問它時,OS分頁和操作系統仍然會返回舊文件。您可以通過每次刷新時生成唯一的文件名解決此問題

+0

是的,我已檢查新圖像正在創建和標籤只是保持其原始形式(第一個圖像生成).....我將如何去創建獨特的文件名? –

+0

您可以在創建時附加隨機數字和文件名稱,以使其具有唯一性。稍後在閱讀時使用相同的名稱。 – Azhar