简体   繁体   English

如何生成PrintWriter并将内容写入JTextArea或相等

[英]how to generate a PrintWriter and write stuff to a JTextArea or equal

I have a Problem, I dont know how to create a PrintWriter and then write the stuff to a JTextArea . 我有一个问题,我不知道如何创建PrintWriter ,然后将其写入JTextArea

I have a library which want a PrintWriter in a method which I want to use to get some output. 我有一个库,该库需要一种我想用来获取输出的方法中的PrintWriter。 But I dont know how to generate a instance of the PrintWriter and then add the information to a JTextArea. 但是我不知道如何生成PrintWriter的实例,然后将信息添加到JTextArea。

Anybody can help me? 有人可以帮助我吗?

I need s.th. 我需要…… like this: 像这样:

PrintWriter pw = new PrintWriter(...);
foo.print(pw);

PrintWriter can wrap another Writer and pass along everything written to it. PrintWriter可以包装另一个Writer并传递写入它的所有内容。 You can use a StringWriter to write to a string buffer, then get the contents of the buffer using toString when you're done. 您可以使用StringWriter写入字符串缓冲区,然后在完成后使用toString获取缓冲区的内容。

StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
foo.print(writer);

String contents = buffer.toString();
myTextArea.setText(contents);

This only writes to the textarea once the library method finishes writing to the PrintWriter though. 不过,仅在库方法完成对PrintWriter写入后,这才写入textarea。 Did you need it to update the textarea while the library method is executing? 在执行库方法时是否需要它来更新文本区域?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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