【问题标题】:how can i add the on hover effect on hyperlink in jeditorpane using swing in javajava - 如何使用java中的swing在jeditorpane中的超链接上添加悬停效果
【发布时间】:2016-02-22 13:21:56
【问题描述】:

使用Hyperlink in jEditorPane 链接,我设法在 jEditorPane 中获取了超链接。

但现在我想将悬停效果添加到超链接。 我尝试使用

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 效果呢?

【问题讨论】:

    标签: java html css swing hyperlink


    【解决方案1】:

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

    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,这有助于为我的代码添加一个新功能,用于鼠标悬停时的颜色组合。
    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多