如何在eclipse中打印到textArea而不是控制台? [英] How to print to textArea instead of console in eclipse?

查看:853
本文介绍了如何在eclipse中打印到textArea而不是控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我现在在一个单独的类中添加一个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){
//将行写入组件
}
} catch(IOException ex){
//处理ex
}
}

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


I currently have a program that prints lines of text to the screen in various manners such as 'System.out.println()' statements and for loops the print all elements in an array to screen.

I am now adding a GUI to this program in a seperate class. My problem is that I want to print everything that prints to eclipse's console to a textbox in my GUI instead. Is this possible and if so how would I go about doing this.

Thanks.

解决方案

If you really want to do this, set the System OutputStream to a PipedOutputStream and connect that to a PipedInputStream that you read from to add text to your component, for example:

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

You can then read from the reader and write it to your text component, for example:

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

I'd suggest that you don't use System.out for your application output though, it can be used by anything (e.g. any third party libraries you decide to use). I'd use logging of some sort (java.util.logging, Log4J etc) with an appropriate appender to write to your component.

这篇关于如何在eclipse中打印到textArea而不是控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆