这有几个部分:
正确设置JEditorPane
在
JEditorPane需要有上下文类型
text/html,并且它需要不可编辑的链接,可以点击:
final JEditorPane editor = new JEditorPane();editor.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));editor.setEditable(false);添加链接
您需要将实际
<a>标签添加到编辑器中,以便将它们呈现为链接:
editor.setText("<a href="http://www.google.com/finance?q=NYSE:C">C</a>, <a href="http://www.google.com/finance?q=NASDAQ:MSFT">MSFT</a>");添加链接处理程序
默认情况下,单击链接不会执行任何操作。您需要
HyperlinkListener与他们打交道:
editor.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {// Do something with e.getURL() here } }});您如何启动浏览器进行处理
e.getURL()取决于您自己。如果您使用Java
6和受支持的平台,则一种方法是使用
Desktop类:
if(Desktop.isDesktopSupported()) { Desktop.getDesktop().browse(e.getURL().toURI());}


