简体   繁体   English

我怎样才能获得JTextPane的内容?

[英]how can I get the content of my JTextPane?

Hy everyone! 大家好! I'm stuck in a little problem. 我陷入了一个小问题。

I need some help or an idea, something...I have some words in a JTextPane and I want to read them one by one. 我需要一些帮助或想法,某些东西......我在JTextPane有一些单词,我想逐一阅读它们。

I know I have to do it with StringTokenizer or with this 我知道我必须使用StringTokenizer或使用它

Element doc=textPane.getDocument().getDefaultRootElement() , Element doc=textPane.getDocument().getDefaultRootElement()

Thank you for any idea, or help. 感谢您的任何想法或帮助。 Any suggestion would be appreciated 任何建议将不胜感激

You can get the text from a JTextPane with getText() : 您可以使用getText()JTextPane获取文本:

String text = txtpane.getText();

Then, if you want to get each word, you could split around each non -word character using a regex: 然后,如果你想获得每个单词,你可以使用正则表达式分割每个单词字符:

String[] words = text.split("\\W"); // "\\W" is \W, which is non-word characters

If you just want to do it based on whitespace, you could use: 如果您只想基于空格进行操作,可以使用:

String[] words = text.split("\\s"); // "\\s" is \s, which is whitespace

Then, to "read them one by one," iterate over each element in the array: 然后,要“逐个读取它们”,迭代数组中的每个元素:

String text = txtpane.getText();
String[] words  text.split("\\W");
// or: String[] words = txtpane.getText().split("\\W")
for (String word : words) {
    System.out.println(word);
    // do whatever
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM