你需要一個TextAreaOutputStream,例如
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* TextAreaOutputStream creates an outputstream that will output to the
* given textarea. Useful in setting System.out
*/
public class TextAreaOutputStream extends OutputStream {
public static final int DEFAULT_BUFFER_SIZE = 1;
JTextArea mText;
byte mBuf[];
int mLocation;
public TextAreaOutputStream(JTextArea component) {
this(component, DEFAULT_BUFFER_SIZE);
}
public TextAreaOutputStream(JTextArea component, int bufferSize) {
mText = component;
if (bufferSize < 1) bufferSize = 1;
mBuf = new byte[bufferSize];
mLocation = 0;
}
@Override
public void write(int arg0) throws IOException {
//System.err.println("arg = " + (char) arg0);
mBuf[mLocation++] = (byte)arg0;
if (mLocation == mBuf.length) {
flush();
}
}
public void flush() {
mText.append(new String(mBuf, 0, mLocation));
mLocation = 0;
}
}
創建它,然後使用System.setOut(OutputStream中)到您的System.out發送到文本區域
謝謝您的回答。 。它正在工作.. – user5809644
謝謝,但ControlAltDel是正確的:我應該重定向系統輸出。我調整了我的答案。新代碼中的 – JensS
「System.out.println(」hello「);」不顯示..輸出...只有jframe顯示... – user5809644