Java 如何将for(CoreLabel-token:句子.get(TokensAnnotation.class)){}循环中的标准输出重定向到jTextField?

Java 如何将for(CoreLabel-token:句子.get(TokensAnnotation.class)){}循环中的标准输出重定向到jTextField?,java,netbeans,nlp,Java,Netbeans,Nlp,我是java编程的初学者,正在使用java进行nlp项目。下面是代码。我通过System.out.println获得正确的输出。如何将标准输出重定向到“for”(CoreLabel标记:句子.get(TokensAnnotation.class)){ }“循环到jTextField?.setText仅显示最后一个值,而.append显示错误消息。请帮助。” import java.util.*; import edu.stanford.nlp.pipeline.*; import edu.st

我是java编程的初学者,正在使用java进行nlp项目。下面是代码。我通过System.out.println获得正确的输出。如何将标准输出重定向到“for”(CoreLabel标记:句子.get(TokensAnnotation.class)){ }“循环到jTextField?.setText仅显示最后一个值,而.append显示错误消息。请帮助。”

import java.util.*; 
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.ling.*; 
import edu.stanford.nlp.ling.CoreAnnotations.*;  
import edu.stanford.nlp.util.CoreMap;
import java.io.OutputStream;
public class Nlp extends javax.swing.JFrame {
public Nlp() {
        initComponents();
    }
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 Properties props = new Properties(); 
 props.put("annotators", "tokenize, ssplit, pos, lemma"); 
 StanfordCoreNLP pipeline = new StanfordCoreNLP(props, false);
 String text = jTextField1.getText();
 Annotation document = pipeline.process(text);  
 for(CoreMap sentence: document.get(SentencesAnnotation.class))
  {    
      for(CoreLabel token: sentence.get(TokensAnnotation.class))
       {   
             String word = token.get(TextAnnotation.class);     
              System.out.println("Tokens :" + word);           
         }       
   }   
}
 public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Nlp().setVisible(true);
            }
        });
}
private javax.swing.JButton jButton1;
 private javax.swing.JTextField jTextField1;
 private javax.swing.JTextField jTextField2;
 private javax.swing.JTextField jTextField3;
 private javax.swing.JTextField jTextField4;
}

您应该尝试创建一个字符串缓冲区,从中创建所有迭代中的文本,然后在所需的文本字段中设置该文本

例如,使用您的代码:

// create a buffer
StringBuilder builder = new StringBuilder();

for(CoreMap sentence: document.get(SentencesAnnotation.class))
  {    
      for(CoreLabel token: sentence.get(TokensAnnotation.class))
       {   
             String word = token.get(TextAnnotation.class);     
             // System.out.println("Tokens :" + word);   
            // append each iteration text to the buffer
            builder.append("Tokens :");
            builder.append(word);
            builder.append("\n");
         }       
   }   
}

// show the buffer in text field
someJTextField.setText( builder.toString());

如果您收到错误消息,请发布它,以便其他人可以使用它来帮助您诊断问题。非常有用,先生。它可以工作。但是builder.append(“\n”);没有给出新行。我还尝试了builder.append(System.getProperty(“line.separator”);