简体   繁体   中英

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?

Thank you.

Versions of Java since 1.5 have System.setOut() which allow you to install your own PrintStream . Just create a simple OutputStream which appends the data it get through write() Then wrap it in a PrintStream and install it.

那么你可以使用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. If you have a method like (say) MyUtil.myOutput() then you can make a single change and reroute it where you want

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM