简体   繁体   English

如何在JEditorPane中显示可点击的链接

[英]how to display clickable links in JEditorPane

I am trying to display a list of clickable links in JEditorPane. 我想在JEditorPane中显示可点击链接列表。 Here is my code: 这是我的代码:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;


public class GUI extends JFrame{
    JEditorPane editorpane=new JEditorPane();
    //this is the constructor
    GUI(){
        JFrame frame=new JFrame("Frame");

        frame.add(editorpane);
        JScrollPane scroll=new JScrollPane(editorpane);

        editorpane.setContentType("text/html");
        editorpane.setEditable(false);
        editorpane.setText("<html><body>Test <a href='http://www.java.net'>"
+ "www.java.net</a></body></html>");
        StyleSheet css = ((HTMLEditorKit)
        editorpane.getEditorKit()).getStyleSheet();
        Style style = css.getStyle("body");
        editorpane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() ==
        HyperlinkEvent.EventType.ACTIVATED) {
        System.out.println("Open browser: " + e.getURL());
        }
        }
        });
        frame.setSize(512, 342);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(scroll);
        frame.show();
    }
    public void append(String s) {
    try{
        Document doc = editorpane.getDocument();
        doc.insertString(doc.getLength(), "\n", null);
        doc.insertString(doc.getLength(), s, null);

    }
    catch(BadLocationException exc){
    }
    }
    //main method
    public static void main(String args[]){

    GUI gui=new GUI();
    gui.append("<html><body>Test <a href='http://www.java.net'>"
+ "www.java.net</a></body></html>");


    }
}

It is showing a clickable link, when I used setText() method in the constructor, but then when I tried to display additional link with append(); 它显示了一个可点击的链接,当我在构造函数中使用setText()方法时,但是当我尝试使用append()显示其他链接时 method it is showing the html tags along with the text and it doesn't make my url a hyperlink. 方法它显示html标签以及文本,它不会使我的网址成为超链接。 Any ideas, why is it not working with append? 任何想法,为什么它不兼容追加?

Use either method of HTMLEditorKit 使用HTMLEditorKit的任一方法

public void insertHTML(HTMLDocument doc, int offset, String html,
               int popDepth, int pushDepth,
               HTML.Tag insertTag) 

Or methods of HTMLDocument 或者HTMLDocument的方法

public void insertAfterEnd(Element elem, String htmlText)
public void insertAfterStart(Element elem, String htmlText)
public void insertBeforeStart(Element elem, String htmlText
public void insertBeforeEnd(Element elem, String htmlText)

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

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