JEdi​​torPane Hyperlink swing html

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

bottomText.setText("Yahoo"); 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(); } } } }); 

哇,那比我更简单:P

 // Move this //bottomText.setText("Yahoo"); bottomText.setEditable(false); bottomText.setOpaque(false); bottomText.setEditorKit(JEditorPane.createEditorKitForContentType("text/html")) // To Here bottomText.setText("Yahoo"); 

哦,等到用户在打开浏览器之前点击了链接,在我杀了你之前有大约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(); } } } } }); 

bottomText.setEditorKit之前调用bottomText.setText