0

我为 JEditorPane 实现 DocumentListener(需要方法 insertUpdate(DocumentEvent e) 的帮助)。如何阅读最后一个单词或最后一行(行由 '\n' 分隔,单词由 ' ' 分隔)?

4

2 回答 2

3

实用程序类可以与格式化的内容一起使用,例如 html

public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException 
public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException 
于 2011-04-13T09:54:56.013 回答
2

要获取 JEditorPane 中的最后一行,请在编辑器中拆分文本,\n如下所示:

String text = editor.getText();
String[] lines = text.split("\n");
String lastLine = lines[lines.length-1];
System.out.println("Last line: " + lastLine);

同样,要获取最后一个单词,请在空格上拆分最后一行。

于 2011-04-13T09:06:07.873 回答