2013-05-10 108 views
0

大家好!我陷入了一个小问题。如何获取我的JTextPane的内容?

我需要一些帮助或想法,有些东西......我在JTextPane中有一些词,我想逐一阅读它们。

我知道我有StringTokenizer的或与本

Element doc=textPane.getDocument().getDefaultRootElement()做到这一点,

谢谢你的任何想法,或帮助。任何建议,将不胜感激

+0

如果“的话”你的意思是用空格分隔文本字符串,你可以使用'split'方法。见http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String) – 2013-05-10 14:23:10

回答

2

您可以getText()得到一个JTextPane文本:

String text = txtpane.getText(); 

然后,如果你想获得每个单词,你可以使用一个围绕每个 -word性格分裂正则表达式:

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

如果你只是想根据空格做到这一点,你可以使用:

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

然后,“通过一个阅读一个”阵列中遍历每个元素:

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 
} 
+0

非常感谢您alex23和WChargin – 2013-05-10 14:28:29

+0

欢迎的StackOverflow!如果您发现此答案有帮助,请考虑将其投出(灰色向上箭头)或将其标记为接受的答案(如果它完全解决了您的问题)(绿色复选标记轮廓)。 – wchargin 2013-05-10 14:29:13

+0

你的帖子真的帮了我 – 2013-05-10 14:29:23