簡體   English   中英

如何使我的JLabel圖標刷新?

[英]how can I get my JLabel icon to refresh?

我有一個程序可以在輸入數據后顯示靜態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;
    }




}

這是代碼的可運行版本在這里

            lblMap.setIcon(null);
            map.genimage();
            ImageIcon im = new ImageIcon("map.jpg");
            lblMap.setIcon(im);

看起來您正在創建map.jpg並在需要刷新時將其綁定到標簽。 使用此代碼,刷新后您仍然看到舊圖像嗎? 您是否已驗證map.jpg是最新版本? 如果是,那么操作系統分頁可能會出現問題,並且當JVM請求時,操作系統仍會返回舊文件。 您可以通過在每次刷新時生成唯一的文件名來解決此問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM