【问题标题】:Open telephone number link打开电话号码链接
【发布时间】:2021-05-29 17:43:01
【问题描述】:

我想打开 MS Teams 以拨打带有 <a href="tel:..... 标签的 HTML 网页上的号码。 据我所知,Java 中的 HTML 版本非常旧,可能包含的版本不支持 tel:。但是,我尝试了这段代码,它适用于普通 HMTL 链接。

String st = "<html>\r\n<a href=\"tel:+4917312345678\">0173 12345678</a>\r\n</html>";
editorPane = new JEditorPane("text/html", st);
// handle link events
editorPane.addHyperlinkListener(new HyperlinkListener() {
    @Override
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
                try {
                    Desktop.getDesktop().browse(e.getURL().toURI());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (URISyntaxException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } // roll your own link launcher or use Desktop if J6+
    }
});
editorPane.setEditable(false);
editorPane.setBounds(72, 244, 423, 137);
panel_3.add(editorPane);

但这不适用于tel: 链接

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toURI()" because the return value of "javax.swing.event.HyperlinkEvent.getURL()" is null

有什么想法吗?

【问题讨论】:

标签: java swing jeditorpane tel


【解决方案1】:

好的,我找到了一种使用 tel: 链接的方法。 我的代码仅适用于 Windows,因此如果您想跨平台使用,您必须先进行检查,例如 here

    String st = "<html>\r\n<a href=\"tel:+4917312345678\">+4917312345678</a>\r\n</html>";
    editorPane = new JEditorPane("text/html", st);
    // handle link events
    editorPane.addHyperlinkListener(new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
                    try {
                        Runtime rt = Runtime.getRuntime();
                        String url = e.getDescription();
                        System.out.println(url);
                        rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
        }
    });
    editorPane.setEditable(false);
    editorPane.setBounds(72, 244, 423, 137);
    panel_3.add(editorPane);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2018-08-12
    相关资源
    最近更新 更多