我遇到以下问题:我想将内容类型为 text/html
的 JTextPane
的光标更改为 Cursor.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);
}
});
}
}
非常感谢!
最佳答案
因此,经过更多的挖掘后,似乎是 EditorKit
(在本例中为 HTMLEditorKit
)负责决定应该使用什么光标。
您可以使用以下命令更改“默认”光标...
jTextPane1.setContentType("text/html");
((HTMLEditorKit)tp.getEditorKit()).setDefaultCursor(cursor);
默认的“default”被定义为private static final Cursor DefaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
这非常烦人......
关于java - 带 HTML 的 JTextPane 文本光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29310442/