我使用的庫有public void
方法init()
其中有System.out.println()
。如何使用system.out.println的void方法填充Jtextarea?
而在我的實現中,我只能訪問方法init()
這是無效的,但無法從sout輸出到我的gui組件JTextarea
。
是否有可能趕上它(與InputStream
或以其他方式)?
任何幫助表示讚賞。
感謝
我使用的庫有public void
方法init()
其中有System.out.println()
。如何使用system.out.println的void方法填充Jtextarea?
而在我的實現中,我只能訪問方法init()
這是無效的,但無法從sout輸出到我的gui組件JTextarea
。
是否有可能趕上它(與InputStream
或以其他方式)?
任何幫助表示讚賞。
感謝
由於從其他正確答案的一些異常錯誤,我決定去遠一點看,發現下面的精彩類,完美的作品!
如果你想獲得幻想,你可以使用System.setOut(PrintStream out)
重新路由System.Out
你自己PrintStream
。此時,您可以使用InputStreamReader
來閱讀PrintStream
。然後InputStreamReader
可以填充您的GUI。
這不是在JVM中進行通信的推薦方式,但如果這是您的庫提供給您的,我想這就是您必須使用的。
但請記住,如果您重新路由System.Out
,所有使用System.Out
的方法將使用新的PrintStream
。這意味着什麼都不會打印到您的控制檯了。
這裏有一個例子就是我在談論:
import java.io.*;
import java.util.Arrays;
import javax.swing.*;
public class StandardOutReader extends JPanel{
JTextArea textArea = new JTextArea();
public StandardOutReader(){
add(textArea);
try {
//create all your inputs/outputs
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(out);
System.setOut(new PrintStream(out));
final InputStreamReader reader = new InputStreamReader(in);
//A thread that will continuously read the reader, and post the output to the GUI
new Thread(new Runnable(){
@Override
public void run() {
try {
char[] cBuffer = new char[256]; //Buffer for reading
int bytesRead; // number of byts read in the last "read()" call
while((bytesRead = reader.read(cBuffer)) != -1){
//Copy over only the interesting bytes
final char[] copy = Arrays.copyOf(cBuffer, bytesRead);
//Tell the GUI to update
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
textArea.append(new String(copy));
}});
}
} catch (IOException e) {
e.printStackTrace();
} finally{
//Closing the reader
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new StandardOutReader());
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//Thread that randomly prints stuff. Not part of the GUI stuff at all.
new Thread(new Runnable(){
@Override
public void run() {
for(int i = 0; i < 10; i++){
try {
Thread.sleep((long)(4000 * Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Adding another phrase");
}
}}).start();
}
}
嗨,尼克,是否有可能實時獲得?也許使用Swingworker或Listener。我發現有addMessageListener(偵聽器
@feruz你可以有一個坐在while循環中的線程(不在EDT中),不斷從'InputStreamReader'讀取數據。只要它讀取,它就可以發佈到GUI(使用'SwingUtilities.invokeAndWait(...)')。然後,所有內容都將盡可能實時。 –
很好,謝謝...它工作,雖然我得到了異常:'java.io.IOException:寫入結束死亡 \t在java.io.PipedInputStream.read(PipedInputStream.java:311) \t at java.io. PipedInputStream.read(PipedInputStream.java:378) \t在sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) \t在sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) \t (InputStreamReader。)中的sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) \t(java.io.InputStreamReader.read(InputStreamReader。java:184) \t at java.io.Reader.read(Reader.java:140)' – feruz
假設你沒有訪問到圖書館可以改變它(我是有點懷疑使用打印到系統庫的.OUT一個GUI應用程序),你可以做到以下幾點:
PrintStream oldOut = System.out;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newOut = new PrintStream(baos);
System.setOut(newOut);
OtherLib.init();
textArea.setText(baos.toString());
} finally {
System.setOut(oldOut);
}
當你有相同的答案,這是一個2分鐘的差異,你不討厭它嗎? +1代碼示例。並巧妙地使用'ByteArrayOutputStream'(我不知道'toString()'技巧) –
這太棒了,謝謝beny23和其他人!雖然,是否有可能實時獲得輸出?它正在工作/獲取文本到TextArea操作完成後.. – feruz
爲什麼不只是使用'textArea.setText(字符串)'而非'System.out.print'? – Christian
不要將GUI編程與控制檯編程混合使用。 –
如果這是一個'Applet',打開控制檯;顯示相關的調試策略[here](http://stackoverflow.com/q/868111/230513)。 – trashgod