从 textArea 中检索行 - Java

Retrieving lines from textArea - Java

我创建了一个文本区域,默认情况下有两行文本 该程序是 运行。用户应该输入任何单词,然后按回车键。 他们可以根据需要多次执行此操作——这意味着他们输入的最后一个词不一定总是在同一行上。 我希望能够访问他们输入的最后一个单词,该单词位于最后一行,但是我找不到任何方法来这样做。如有任何建议,我们将不胜感激!

I want to be able to access the last word they typed, which would be on the last line,

您可以使用 Utilities class 来帮助您:

int end = textArea.getDocument().getLength();
int start = Utilities.getRowStart(textArea, end);

while (start == end)
{
    end--;
    start = Utilities.getRowStart(textArea, end);
}

String text = textArea.getText(start, end - start);
System.out.println("(" + text + ")");

以上将 return 包含文本的最后一行。 while 循环处理文本区域末尾的空行。