使用JFrame显示Google地图

时间:2013-12-02 11:00:16

标签: java swing jframe

如何使用JFrame显示Google地图?这段代码有什么错误?它没有显示正确的位置。

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class hyperlink extends JFrame {

    public static void main(String arg[])throws Exception {
        new hyperlink();
    }

    public hyperlink() throws Exception {
        String s = "https://maps.google.com/maps?z=10&q=36.26577+-92.54324";
        JEditorPane pane = new JEditorPane(s);
        pane.setEditable(false);
        final JEditorPane finalpane = pane;
        pane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent r) {
                try {
                    if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                        finalpane.setPage(r.getURL());
                } catch(Exception e) {}
            }
        });
        setContentPane(new JScrollPane(pane));
        setSize(1000,1000);
        setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:4)

JEditorPane从未打算成为通用浏览组件。您可以尝试基于Java-FX的WebView,但我不确定这是否更好。

答案 1 :(得分:1)

如果您只是想显示地图,最好将它们显示为图像。您将需要一个浏览器组件,它将显示谷歌的动态地图。此外,javascript兼容性和地图服务更新可能存在其他问题。

Google提供了一种将地图下载为图片的方法:

https://developers.google.com/maps/documentation/staticmaps/#quick_example