我想在JPanel中插入超链接(以便点击我转到网址) 可能吗? 如果是,我该怎么做?我想在JPanel中插入超链接
0
A
回答
1
我们一直在使用这样的事情:
public class UrlTextPane extends JTextPane {
private final Pattern urlPattern = Pattern.compile(UrlUtil.URL_REGEX);
public UrlTextPane() {
this.setEditable(false);
this.addHyperlinkListener(new UrlHyperlinkListener());
this.setContentType("text/html");
}
private class UrlHyperlinkListener implements HyperlinkListener {
@Override
public void hyperlinkUpdate(final HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
Desktop.getDesktop().browse(event.getURL().toURI());
} catch (final IOException e) {
throw new RuntimeException("Can't open URL", e);
} catch (final URISyntaxException e) {
throw new RuntimeException("Can't open URL", e);
}
}
}
};
@Override
/**
* Set the text, first translate it into HTML:
*/
public void setText(final String input) {
final StringBuilder answer = new StringBuilder();
answer.append("<html><body style=\"font-size: 8.5px;font-family: Tahoma, sans-serif\">");
final String content = StringEscapeUtils.escapeHtml(input);
int lastIndex = 0;
final Matcher matcher = urlPattern.matcher(content);
while(matcher.find()) {
//Append everything since last update to the url:
answer.append(content.substring(lastIndex, matcher.start()));
final String url = content.substring(matcher.start(), matcher.end()).trim();
if(UrlUtil.isValidURI(url)) {
answer.append("<a href=\"" + url + "\">"+url+"</a>");
} else {
answer.append(url);
}
lastIndex = matcher.end();
}
//Append end:
answer.append(content.substring(lastIndex));
answer.append("</body></html>");
super.setText(answer.toString().replace("\n", "<br />"));
}
}
0
3
+0
@ stacker我想,当我点击超链接,我去提到的地址。那里没有解释 – 2011-04-18 14:53:21
0
我写了一个简单的函数,它的文本,链接,你要显示的位置,并返回一个JLabel
。当鼠标悬停在文本上时,它会变成蓝色并带有下划线,并使光标成为指针,否则它会变黑,并且不带下划线和默认光标。
public static JLabel makeHyperLink(final String s, final String link, int x, int y)
{
final JLabel l = new JLabel(s);
l.addMouseListener(new MouseAdapter()
{
@Override
public void mouseExited(MouseEvent arg0)
{
l.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
l.setText(s);
}
@Override
public void mouseEntered(MouseEvent arg0)
{
l.setCursor(new Cursor(Cursor.HAND_CURSOR));
l.setText(String.format("<HTML><FONT color = \"#000099\"><U>%s</U></FONT></HTML>", s));
}
@Override
public void mouseClicked(MouseEvent arg0)
{
try
{
URI uri = new URI(link);
if (Desktop.isDesktopSupported())
Desktop.getDesktop().browse(uri);
} catch (Exception e)
{
}
}
});
l.setBounds(x, y, s.length()*5, 20);
l.setToolTipText(String.format("go to %s", link));
return l;
}
如果代码的任何一部分不明确,联系我;)
相关问题
- 1. 如何在UITextView中插入超链接?
- 2. 在Outlook主体中插入超链接
- 3. 超链接插入DIV
- 4. 插入超链接,并删除超级链接在Javascript
- 5. 将超链接插入到XWPFTableRow中
- 6. 插入超链接(CSS) - WordPress的博客
- 7. SSRS 2005:无法插入超链接
- 8. 从表单插入超链接到pdf
- 9. 在JPanel中插入按钮
- 10. 想要XML文件中的超链接
- 11. 如何在GWT中将图像插入到超链接中
- 12. 我想在cakephp中的电子邮件中插入一个链接
- 13. 在pyplot交互模式中插入超链接
- 14. 是否可以在ReST文字块中插入超链接?
- 15. 在watson对话框对话框中插入超链接
- 16. 如何在标签文本中插入超链接?
- 17. 在WPF中的指定位置插入超链接FlowDocument
- 18. 在wysihtml5编辑器中插入超链接时设置焦点
- 19. 在邮件正文中插入文本,超链接和表
- 20. 插入链接
- 21. 在Flowplayer中嵌入超链接
- 22. 我想要超链接在不同的地方
- 23. 我想在浏览器中打开新标签,当我按下asp.net超链接
- 24. 想要使不活动的超链接
- 25. 我不想在我的列表视图中超过30个插入
- 26. 将超链接插入范围的一部分(单词中)
- 27. 如何将超链接插入到Outlook Appointment的RTFBODY属性中?
- 28. 如何在JLabel中插入JPanel?
- 29. 我想用一个链接
- 30. 我想从以下链接
UrlUtil包含一个URL正则表达式和一个方法,试图解析URL到一个Java URI(这是非常严格的)。 – 2011-04-18 13:35:34