ceSelection方法的JEditorPane中的可单击HT

ceSelection方法的JEditorPane中的可单击HT

本文介绍了使用replaceSelection方法的JEditorPane中的可单击HTML链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了如何在JEditorPane中创建可点击的链接,然后发现了这个问题:

I searched how to make clickable links in JEditorPane, and I found this question:

这非常有用,但是我的代码使用重复语句在循环中添加链接:

It was very useful, but my code uses a repetition statement to add links in a loop:

JEditorPane jep = new JEditorPane();
jep.setContentType("text/html");
jep.setEditable(true);// Because replaceSelection can't work with disabled edit
for ( int i = 1; i <= 3; i++ ){
    jep.replaceSelection(
        "Welcome to <a href='https://stackoverflow.com/'>StackOverflow i </a>.");
}
jep.setEditable(false);

现在,它只显示没有可点击链接的文本.我该怎么做才对?我真的需要replaceSelection方法.

Now it shows me just text without clickable links. How am I going to make it right? I really need replaceSelection method.

推荐答案

HTMLDocument上使用replaceSelection()会插入原始字符串.您要插入HTML锚标记.可以,

Using replaceSelection() on an HTMLDocument inserts the raw string; you want to insert an HTML anchor tag. You can,

  • 自己管理原始HTML文本,如下所示,然后让setText()处理解析.

使用现有的 HTMLEditorKit 嵌套操作.

Use one of the existing HTMLEditorKit nested actions.

使用此处中看到的自定义方法之一.

Use one of the custom approaches seen here.

import java.awt.Desktop;
import java.awt.HeadlessException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/**
 * @see https://stackoverflow.com/a/16447176/230513
 * @see https://stackoverflow.com/a/14170141/230513
 */
public class Test {

    public static void main(String[] argv) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                display();
            }
        });
    }

    private static String create(int i) {
        StringBuilder sb = new StringBuilder();
        sb.append("Welcome to <a href=");
        sb.append("'http://www.example.com'>Example ");
        sb.append(i);
        sb.append("</a>.<br>");
        return sb.toString();
    }

    private static void display() throws HeadlessException {
        JEditorPane jep = new JEditorPane();
        jep.setContentType("text/html");
        StringBuilder sb = new StringBuilder();
        sb.append("<b>Welcome</b>:<br><hr>");
        for (int i = 1; i <= 3; i++) {
            sb.append(create(i));
        }
        sb.append("<hr>");
        jep.setText(sb.toString());
        jep.setEditable(false);
        jep.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
                    System.out.println(e.getURL());
                    Desktop desktop = Desktop.getDesktop();
                    try {
                        desktop.browse(e.getURL().toURI());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        JFrame f = new JFrame("HyperlinkListener");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(jep);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

这篇关于使用replaceSelection方法的JEditorPane中的可单击HTML链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:31