如何在eclipse中打印到textArea而不是console?

我目前有一个程序,以各种方式打印文本行,如’System.out.println()’语句和循环打印arrays中的所有元素到屏幕。

我现在在一个单独的类中为这个程序添加一个GUI。 我的问题是我想将打印到eclipse控制台的所有内容打印到我的GUI中的文本框中。 这是可能的,如果是这样,我将如何做到这一点。

谢谢。

如果您确实想这样做,请将System OutputStream设置为PipedOutputStream并将其连接到您读取的PipedInputStream以向组件添加文本,例如:

PipedOutputStream pOut = new PipedOutputStream(); System.setOut(new PrintStream(pOut)); PipedInputStream pIn = new PipedInputStream(pOut); BufferedReader reader = new BufferedReader(new InputStreamReader(pIn)); 

然后,您可以从阅读器中读取并将其写入文本组件,例如:

 while(appRunning) { try { String line = reader.readLine(); if(line != null) { // Write line to component } } catch (IOException ex) { // Handle ex } } 

我建议您不要将System.out用于您的应用程序输出,它可以被任何东西使用(例如您决定使用的任何第三方库)。 我会使用某种类型的日志记录(java.util.logging,Log4J等)和适当的appender来写入您的组件。