使用 JEditorPane,有什么方法可以判断插入符当前是否在最后一行?我在 API 中找不到任何内容,更重要的是,在派生 JEditorPane 的 JTextComponent 中找不到任何内容。当用户使用向下箭头键向下移动文本时,我需要找出这一点。我目前的想法是这样的:
private boolean isEndOfText() {
int tmpCurrent = editor.getCaretPosition();
editor.getActionMap().get(DefaultEditorKit.endLineAction).actionPerformed(null);
int tmpEnd = editor.getCaretPosition();
try { editor.setCaretPosition(tmpEnd + 1); } catch (Exception e) { editor.setCaretPosition(tmpCurrent); return true; }
editor.setCaretPosition(tmpCurrent);
return false;
}
只要按下向下键,此代码就会运行,并通过检测插入符号放在最后一个可能位置(即结尾)之后是否发生错误来返回是否确实是文本结尾该行的位置(如果它实际上是最后一行),否则意味着尚未到达文本末尾。
最佳答案
您应该能够使用 Text Utilities 。一种方法返回总行数,另一种方法返回插入符号处的行。
我从来没有过多地使用过 JEditorPane,因为我不喜欢它的 HTML 支持。您还可以使用 editor.getDocument().getLength() 来确定插入符号是否位于文档末尾。这适用于仅显示文本而不是 HTML 的 JTextArea 或 JTextPane。不确定它在 JEditorPane 中如何工作。
关于java - JEditorPane 是最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4128435/