简体   繁体   English

在Java中单击时更改链接的颜色

[英]change color of link when clicked in Java

Okay, I know the title of the post is very familiar. 好的,我知道该帖子的标题非常熟悉。 I saw a lot of posts on this subject, but non of them covered the code in Java. 我看到了很多有关此主题的文章,但没有一篇涉及Java代码。 I have this HyperlinkListener class : 我有这个HyperlinkListener类:

private void editorpaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
   // TODO add your handling code here:
   if (evt.getEventType() ==
                       HyperlinkEvent.EventType.ACTIVATED) {
                                    try {
                                        try {
    java.awt.Desktop.getDesktop().browse(evt.getURL().toURI());
                                           } catch (IOException ex) {
    Logger.getLogger(MyAssistantGUI.class.getName()).log(Level.SEVERE, null, ex);
                                        } 
                                 catch (URISyntaxException ex) {
    Logger.getLogger(MyAssistantGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
              System.out.println("Open browser: " + evt.getURL());

I tried to add Style-sheet to my JEditorPane and then add a:active {color: #f00;} , but it does not work. 我试图将样式表添加到我的JEditorPane ,然后添加a:active {color: #f00;} ,但是它不起作用。 Then i saw in the API that java does not support a:active/hoover ..etc. 然后我在API中看到Java不支持a:active/hoover ..etc。 Can you give me suggestions, on how to change the color when the user clicks the link? 您能给我一些建议,关于用户单击链接时如何更改颜色吗?

In the listener you can get position in the text. 在听众中,您可以在文本中找到位置。 Then cast your JEditorPane's Document to StyledDocument and use getCharacterElement() passing the offset you have. 然后将JEditorPane's DocumentStyledDocument并使用getCharacterElement()传递您拥有的偏移量。 For the Element you can change attributes eg background or foreground using setCharacterAttributes() method. 对于Element您可以使用setCharacterAttributes()方法更改属性,例如背景或前景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM