带有HTML的JTextPane的文本光标

Rol*_*015 2 java jtextpane cursor

我有以下问题:我想将JTextPane内容类型的光标更改text/htmlCursor.TEXT_CURSOR.但是,设置时setCursor(new Cursor(Cursor.TEXT_CURSOR))会被忽略.我也尝试在鼠标监听器中设置光标,但它也直接更改回标准光标.如果内容类型是text/plain,则光标默认为文本光标.有没有人知道如何实现这一目标?我创建了一个SCCEE来显示这种行为:

import java.awt.Cursor;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;


public class TextPaneHtmlCursor extends JFrame {
    private JScrollPane jScrollPane1;
    private JTextPane jTextPane1;     

    public TextPaneHtmlCursor() {
        initComponents();
    }

    private void initComponents() {
        jScrollPane1 = new JScrollPane();
        jTextPane1 = new JTextPane();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jTextPane1.setContentType("text/html");
        jTextPane1.setCursor(new Cursor(Cursor.TEXT_CURSOR));
        jScrollPane1.setViewportView(jTextPane1);
        getContentPane().add(jScrollPane1);
        pack();
    }                    

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextPaneHtmlCursor().setVisible(true);
            }
        });
    }        
}
Run Code Online (Sandbox Code Playgroud)

非常感谢你!

Mad*_*mer 5

因此,经过一点点挖掘后,似乎EditorKit(在这种情况下HTMLEditorKit)负责决定应该使用什么游标.

您可以使用类似的内容更改"默认"光标

jTextPane1.setContentType("text/html");
((HTMLEditorKit)tp.getEditorKit()).setDefaultCursor(cursor);
Run Code Online (Sandbox Code Playgroud)

默认的"默认"被定义为private static final Cursor DefaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);非常烦人...