Java控制台JPanel [英] Java Console JPanel
本文介绍了Java控制台JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello
是否可以在JPanel中绘制java控制台返回的内容?
有你的教程吗?
谢谢
sw
Hello Is it possible to draw in a JPanel what the java console is returning ? have you got a tutorial to follow ? thanks sw
推荐答案
我不记得我在哪里找到了这个,使用类I调用TextAreaOutputStream将输出流输出到JPanel中保存的JTextArea:
I can't remember where I found this, but I have outputted the output stream to a JTextArea held in a JPanel using a class I call TextAreaOutputStream:
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TextAreaOutputStream extends OutputStream {
private final JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
private String title;
public TextAreaOutputStream(final JTextArea textArea, String title) {
this.textArea = textArea;
this.title = title;
sb.append(title + "> ");
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) throws IOException {
if (b == '\r')
return;
if (b == '\n') {
final String text = sb.toString() + "\n";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text);
}
});
sb.setLength(0);
sb.append(title).append("> ");
}
sb.append((char) b);
}
}
然后我将标准输出流重定向到此对象作为亚历克斯在他的回答上面提到。
I then re-direct the standard output Stream to this object as Alex mentions in his answer above.
这篇关于Java控制台JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文