Java 使用getStyleDdocument的超链接无效

Java 使用getStyleDdocument的超链接无效,java,html,swing,hyperlink,jtextpane,Java,Html,Swing,Hyperlink,Jtextpane,如何在JTextPane上创建超链接。代码如下 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.text.BadLocationException; import javax.swing.text.Styl

如何在
JTextPane
上创建超链接。代码如下

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JTextPane;

public class Sample extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sample frame = new Sample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws BadLocationException 
     */
    public Sample() throws BadLocationException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("New label");
        panel.add(lblNewLabel);

        JTextPane textPane = new JTextPane();
        JScrollPane pane = new JScrollPane(textPane);
        contentPane.add(pane, BorderLayout.CENTER);

        textPane.setContentType("text/html");
        textPane.setEditable(false);

        StyledDocument doc = textPane.getStyledDocument();
        doc.insertString(doc.getLength(), "Hello\n", null);
        doc.insertString(doc.getLength(), "<a href=\"\">Cancel</a>\n", null);

    }

}
导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.border.EmptyBorder;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.StyledDocument;
导入javax.swing.JLabel;
导入javax.swing.BoxLayout;
导入javax.swing.JScrollPane;
导入javax.swing.JTextField;
导入javax.swing.JCheckBox;
导入javax.swing.JButton;
导入javax.swing.JTextPane;
公共类示例扩展了JFrame{
私有JPanel内容窗格;
私有JTextField textField;
私有JTextField textField_1;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
样本框架=新样本();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*@BadLocationException
*/
public Sample()引发BadLocationException{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100450300);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setLayout(新的BorderLayout(0,0));
setContentPane(contentPane);
JPanel面板=新的JPanel();
添加(面板,BorderLayout.NORTH);
JLabel lblNewLabel=新JLabel(“新标签”);
面板添加(lblNewLabel);
JTextPane textPane=新的JTextPane();
JScrollPane=新的JScrollPane(textPane);
添加(窗格,BorderLayout.CENTER);
setContentType(“text/html”);
textPane.setEditable(false);
StyledDocument doc=textPane.getStyledDocument();
doc.insertString(doc.getLength(),“Hello\n”,null);
doc.insertString(doc.getLength(),“\n”,null);
}
}

阅读
JEditorPane
API,了解如何向JEditorPane或JTextPane添加
HyperlinkListener

编辑:

我真的不理解HTML操作,但我认为以下方法应该有效:

HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
HTMLDocument doc = (HTMLDocument)textPane.getDocument();
String text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
HTMLEditorKit editorKit=(HTMLEditorKit)textPane.getEditorKit();
HTMLDocument doc=(HTMLDocument)textPane.getDocument();
字符串文本=”;
editorKit.insertHTML(doc,doc.getLength(),text,0,0,null);

它不是关于
HyperLinkListener
。当您尝试运行该程序时,我看到的是html标记而不是超链接。@雇佣军:建议您使用一些替代方法。@雇佣军,您确实应该使用一个JEditorPane来显示html。API有一个指向Swing教程的链接。有关动态添加文本的一些代码,请参见编辑。@camickr还有一件事……如何检查名称“hyperlink”,如
HyperlinkListener
中的示例所示。我基本上有两个链接-保存和取消。我需要为保存和取消分别执行一些操作