2011-04-18 87 views

回答

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

UrlUtil包含一個URL正則表達式和一個方法,試圖解析URL到一個Java URI(這是非常嚴格的)。 – 2011-04-18 13:35:34

0

是可能的,

使用HTML

Add one JLabel use HTML as text並把它添加到面板

+0

@ Jigar喬希的JPanel可以閱讀? – 2011-04-18 13:32:47

+0

我說,添加一個標籤,並將其文本設置爲HTML,然後將其添加到面板 – 2011-04-18 13:36:42

+0

@ Jigar Joshi但我無法點擊它。我該如何做? – 2011-04-18 14:47:57

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; 
} 

如果代碼的任何一部分不明確,聯繫我;)