【问题标题】:JEditorPane Hyperlink swing htmlJEdi​​torPane 超链接 swing html
【发布时间】:2012-07-29 23:35:22
【问题描述】:

我很难让超链接在 JEditorPane 中工作。有人可以告诉我我在这里做错了什么吗?我希望能够单击链接和浏览器以打开该页面。提前致谢。 :D

    bottomText.setText("<a href=\"http://www.yahoo.com\">Yahoo</a>");
    bottomText.setEditable(false);
    bottomText.setOpaque(false);
    bottomText.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
    bottomText.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {

            }
            if(Desktop.isDesktopSupported()) {
                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();
                }
            }

        }

    });

【问题讨论】:

    标签: java swing hyperlink jeditorpane


    【解决方案1】:

    哇,这比我还简单:P

    // Move this
    //bottomText.setText("<a href=\"http://www.yahoo.com\">Yahoo</a>");
    bottomText.setEditable(false);
    bottomText.setOpaque(false);
    bottomText.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"))
    // To Here
    bottomText.setText("<a href=\"http://www.yahoo.com\">Yahoo</a>");
    

    哦,等到用户在打开浏览器之前点击了链接,在我杀了你之前有大约 4 个窗口;)

    点击更新

    你快到了;)

    bottomText.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                if (Desktop.isDesktopSupported()) {
                    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();
                    }
                }
            }
        }
    });
    

    【讨论】:

    • 我该怎么做?我刚开始学习Java,所以我不是很有经验。感谢两位的帮助!
    • 再次感谢先生!我将如何更改 JEditorPane 文本的字体?我不喜欢它现在的样子,所以我想更改为默认的 Arial 文本。再次感谢!
    • @HenryPe 通过 HTML 更改字体
    【解决方案2】:

    bottomText.setText之前调用bottomText.setEditorKit

    【讨论】:

    • 谢谢您,先生,我怎样才能投票/接受你们两个的答案?它不会让我同时接受两个答案或至少投票。
    猜你喜欢
    • 2011-04-11
    • 2013-01-29
    • 2011-06-06
    • 1970-01-01
    • 2019-06-13
    • 2013-05-02
    • 2011-08-20
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多