System.out.println 输出到 JTextArea
我想每次调用 System.out.println 来附加到给定的 JTextArea,而不必更改对 System.out.println 的所有调用...这可能吗?
谢谢。
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?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自 1.5 起的 Java 版本都有
System.setOut()
,它允许您安装自己的PrintStream
。只需创建一个简单的OutputStream
,它附加通过write()
获取的数据,然后将其包装在PrintStream
中并安装。Versions of Java since 1.5 have
System.setOut()
which allow you to install your ownPrintStream
. Just create a simpleOutputStream
which appends the data it get throughwrite()
Then wrap it in aPrintStream
and install it.好吧,您可以使用
jTextArea.append("Your String")
方法来做到这一点Well You can do it using the
jTextArea.append("Your String")
method我认为没有简单的方法。正是出于这种原因,我通常会尝试避免在代码中调用
System.out
。如果您有像(例如)MyUtil.myOutput()
这样的方法,那么您可以进行一次更改并将其重新路由到您想要的位置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. If you have a method like (say)MyUtil.myOutput()
then you can make a single change and reroute it where you want我想你可能会使用某种形式的 AspectJ 来做到这一点,但我认为这可能有点矫枉过正。我要做的是创建一个既可以打印又可以附加的方法。
然后,您可以对
System.out.println
进行全局查找和替换,并将其替换为printAndAppend
I suppose you could potentially use some form of AspectJ to do it, but I think that may be overkill. What I would do though is create a method that would both print and append.
You can then just do a global find and replace for
System.out.println
and replace it withprintAndAppend