我需要在swing的文本区域中添加包含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.
}
}
});
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
。 - itroC:\Users\ash>java version 错误:注册表项“Software\JavaSoft\Java Runtime Environment\CurrentVersion”具有值“1.7.0_01”,但需要“1.7”。 错误:找不到java.dll 错误:找不到Java SE Runtime Environment。
- itro