1

我很难让超链接在 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();
                }
            }

        }

    });
4

2 回答 2

3

哇,那比我更简单: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();
                }
            }
        }
    }
});
于 2012-08-01T05:48:17.550 回答
3

bottomText.setEditorKit之前打电话bottomText.setText

于 2012-08-01T05:52:33.797 回答