【发布时间】:2012-03-07 20:29:36
【问题描述】:
我需要在包含 mailto 链接的 swing 文本区域中添加行,单击它应该会打开电子邮件应用程序。
我该怎么做?
【问题讨论】:
-
你应该尝试 JTextPane 而不是 JTextArea。
-
我在 textArea 中添加了文本,你能举个例子吗?
标签: java swing hyperlink textarea mailto
我需要在包含 mailto 链接的 swing 文本区域中添加行,单击它应该会打开电子邮件应用程序。
我该怎么做?
【问题讨论】:
标签: java swing hyperlink textarea mailto
正如我在评论中建议的那样你应该尝试 JTextPane 而不是 JTextArea。
为了使超链接正常工作,您需要做以下事情:
快速演示如下:
final JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setContentType("text/html");
textPane.setText("File not found please contact:<a href='mailto:michael@uml.com'>e-mail to</a> or call 9639");
textPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println(e.getURL());
// write your logic here to process mailTo link.
}
}
});
通过java打开邮件客户端的例子:
try {
Desktop.getDesktop().mail(new URI(e.getURL() + ""));
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
【讨论】:
Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: java/awt/Desktop
C:\Users\ash>java version Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' has value '1.7.0_01', but '1.7' is required. Error: could not find java.dll Error: Could not find Java SE Runtime Environment.