【问题标题】:How to add lines to a textarea which contains mailto links in java?如何将行添加到包含 java 中的 mailto 链接的文本区域?
【发布时间】:2012-03-07 20:29:36
【问题描述】:

我需要在包含 mailto 链接的 swing 文本区域中添加行,单击它应该会打开电子邮件应用程序。

我该怎么做?

【问题讨论】:

  • 你应该尝试 JTextPane 而不是 JTextArea。
  • 我在 textArea 中添加了文本,你能举个例子吗?

标签: java swing hyperlink textarea mailto


【解决方案1】:

正如我在评论中建议的那样你应该尝试 JTextPane 而不是 JTextArea

为了使超链接正常工作,您需要做以下事情:

  • 使 textPane 可编辑 = false。
  • 向其中添加一个 HyperlinkListener,以便您可以监控链接激活事件。

快速演示如下:

    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
  • 这是我的 java 版本:C:\Users\ash&gt;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.
猜你喜欢
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 2020-07-20
  • 2015-01-10
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 2013-11-15
相关资源
最近更新 更多