在jEditorPane中加入链接字符串:
jEditorPane.setText("<a href='http://www.google.com'> Google</a>);如果是本地文件,可把"http://"改为"file://"
然后为jEditorPane添加监听事件:
jEditorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
String command = "explorer.exe "//如果是记事本打开,可用 notepad.exe
+ e.getURL().toString();
Runtime.getRuntime().exec(command);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
});