[英]System.out.println output to JTextArea
I would like to everytime I call System.out.println to append to a given JTextArea, without having to change all calls to System.out.println... Is this possible? 我想每次调用System.out.println追加到给定的JTextArea,而不必更改对System.out.println的所有调用......这可能吗?
Thank you. 谢谢。
Versions of Java since 1.5 have System.setOut()
which allow you to install your own PrintStream
. 自1.5以来的Java版本具有
System.setOut()
,允许您安装自己的PrintStream
。 Just create a simple OutputStream
which appends the data it get through write()
Then wrap it in a PrintStream
and install it. 只需创建一个简单的
OutputStream
,它通过write()
附加数据然后将其包装在PrintStream
并安装它。
那么你可以使用jTextArea.append("Your String")
方法来做到这一点
I don't think there is a simple way. 我不认为有一个简单的方法。 I generally try to avoid
System.out
calls in my code for exactly this sort of reason. 我通常会尝试在我的代码中避免使用
System.out
调用,原因就是这种情况。 If you have a method like (say) MyUtil.myOutput()
then you can make a single change and reroute it where you want 如果您有一个像(比方说)
MyUtil.myOutput()
那么您可以进行一次更改并将其重新路由到您想要的位置
I suppose you could potentially use some form of AspectJ to do it, but I think that may be overkill. 我想你可能会使用某种形式的AspectJ来做这件事,但我认为这可能有点过头了。 What I would do though is create a method that would both print and append.
我会做的是创建一个既可以打印又可以附加的方法。
public void printAndAppend(String text) {
System.out.println(text);
textArea.append(text);
}
You can then just do a global find and replace for System.out.println
and replace it with printAndAppend
然后,您可以只对
System.out.println
执行全局查找和替换,并将其替换为printAndAppend
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.