JEdi​​torPane中的可点击HTML链接但是使用replaceSelcetion方法 [英] Clickable HTML link in JEditorPane But using replaceSelcetion methode

查看:129
本文介绍了JEdi​​torPane中的可点击HTML链接但是使用replaceSelcetion方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了如何在 JEditorPane 中制作可点击链接,我发现此问题

I searched of how to make clickable links in JEditorPane and i found this Question


是否可以用Java创建程序以创建要在Chrome中链接的文本?

这是非常有用但我的代码使用重复声明

It was very useful but my code use a Repetition Statement

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 方法

And now it show me just text without clickable links so how I'm going to make it right and 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);
    }
}

这篇关于JEdi​​torPane中的可点击HTML链接但是使用replaceSelcetion方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆