如何在使用Java Swing的JEditorPane中为超链接添加鼠标悬停效果?

3
使用 jEditorPane 中的超链接,我成功地在 jEditorPane 中添加了超链接。
但现在我想要为超链接添加悬停效果。我尝试使用 CSS 实现,但是…
MyHTMLEditorKit kit = new MyHTMLEditorKit();
setEditorKitForContentType("text/html", kit);        
addHyperlinkListener(createHyperlinkListener());
StyleSheet css= kit.getStyleSheet();
css.addRule("a { color: red;}");

其中MyHTMlEditorKit扩展了HTMLEditorKit。 我尝试了下面的CSS,但它没有起作用。

a:hover{color:red;}

另外,我尝试在超链接中使用onmouseover="this.style.color='red'"属性,但它甚至也没有起作用。

那么我如何在jEditorPane中实现超链接的onhover效果呢?

2个回答

3

这是我的版本,但似乎不是一个好的解决方案。所以我想看看是否有更简洁的方法。

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class HoverEffectTest {
  private static final String SO = "http://stackoverflow.com/";
  private final String s = "<a href='%s' color='%s'>%s</a><br>";
  private final String s1 = String.format(s + "aaaaaaaaaaaaaa<br>", SO, "blue", SO);
  private final String s2 = String.format(s + "cccc", SO, "#0000FF", "bbbbbbbbbbb");
  private final JEditorPane editor = new JEditorPane(
    "text/html", "<html>" + s1 + s2);
  private JComponent makeUI() {
    editor.setEditable(false);
    //@see: BasicEditorPaneUI#propertyChange(PropertyChangeEvent evt) {
    //      if ("foreground".equals(name)) {
    editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    editor.addHyperlinkListener(new HyperlinkListener() {
      @Override public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
          setElementColor(e.getSourceElement(), "red");
        } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
          setElementColor(e.getSourceElement(), "blue");
        } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          Toolkit.getDefaultToolkit().beep();
        }
        //I don't understand why this is necessary...
        //??? call BasicTextUI#modelChanged() ???
        editor.setForeground(Color.WHITE);
        editor.setForeground(Color.BLACK);
      }
    });
    return new JScrollPane(editor);
  }
  private void setElementColor(Element element, String color) {
    AttributeSet attrs = element.getAttributes();
    Object o = attrs.getAttribute(HTML.Tag.A);
    if (o instanceof MutableAttributeSet) {
      MutableAttributeSet a = (MutableAttributeSet) o;
      a.addAttribute(HTML.Attribute.COLOR, color);
    }
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new HoverEffectTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

感谢@aterai的帮助,这有助于我在代码中添加一个新功能,用于鼠标悬停时的颜色组合。 - Foramkumar Parekh

1

try this:

HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a:hover{color:red;}");
Document doc = kit.createDefaultDocument();
String htmlString = "<a href='stackoverflow.com'>Go to StackOverflow!</a>";
// your JEditorPane
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接