[英]Java Make Own System.out
Is there any way to make my own System.out? 有什么办法可以制作自己的System.out吗? I want to have a class "MyConsole" with an attribute out (PrintWriter) and send from all classes txt messages that will be printed in a JTextArea (like System.out print all messages in the console) without make a instance of it in all classes or pass it in the attributes 我想要一个具有属性out(PrintWriter)的类“ MyConsole”,并从所有类中发送将在JTextArea中打印的txt消息(例如System.out在控制台中打印所有消息),而不在所有实例中创建它类或将其传递给属性
i need something like ex: Console1.out.println(message1) and the message 1 goes only in the JTextArea linked Console1, Console2.out.println(message2) and the message 2 goes only in the JTextArea linked Console2 我需要类似ex的东西:Console1.out.println(message1),消息1仅在JTextArea链接的Console1中出现,Console2.out.println(message2),消息2仅在JTextArea链接的Console2中出现
Why don't you just make your own class that extends JTextArea
and write a method to do what you want? 您为什么不只创建自己的扩展JTextArea
的类并编写一种方法来执行所需的工作? Something like: 就像是:
public class MyTextArea extends JTextArea {
public void println() {
//add text to the textarea
}
}
If you don't need your method to have name println
you can just use the append
method, which already exists in the JTextArea
: 如果不需要您的方法使用println
名称,则可以使用JTextArea
已经存在的append
方法:
textArea.append(myString+"\n");
Create a static final class and static methods and use it through out the application. 创建一个静态的final类和静态方法,并在整个应用程序中使用它。 Static method can be directly called without instantiating the class. 无需实例化类即可直接调用静态方法。
I use this open source Message Console in java. 我在Java中使用此开源消息控制台。 It's simple to use. 使用简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.