如何将 标准输入 重定向 到一个 JTextArea?我通过以下代码,将标准输出重定向到了一个 JTextArea1 上面。
目前我想从一个 JTextArea2 上面,如同标准输入一样的进行输入,
每次回车后读取一行,并将读取结果输出到 JTextArea1。
class GUIPrintStream extends PrintStream{
private JTextComponent component;
private StringBuffer sb = new StringBuffer();
public GUIPrintStream(OutputStream out, JTextComponent component){
super(out);
this.component = component;
}
/** *//**
* 重写write()方法,将输出信息填充到GUI组件。
* @param buf
* @param off
* @param len
*/
@Override
public void write(byte[] buf, int off, int len) {
final String message = new String(buf, off, len);
SwingUtilities.invokeLater(new Runnable(){
public void run(){
sb.append(message);
if(sb.length()>4000)sb.delete(0,3000);// 控制 sb 的大小确保不会占用太大内存。
sb.append("\n-------sb:"+sb.capacity()+"-------\n");
component.setText(sb.toString());
}
});
}
}
目前我想从一个 JTextArea2 上面,如同标准输入一样的进行输入,
每次回车后读取一行,并将读取结果输出到 JTextArea1。
class GUIPrintStream extends PrintStream{
private JTextComponent component;
private StringBuffer sb = new StringBuffer();
public GUIPrintStream(OutputStream out, JTextComponent component){
super(out);
this.component = component;
}
/** *//**
* 重写write()方法,将输出信息填充到GUI组件。
* @param buf
* @param off
* @param len
*/
@Override
public void write(byte[] buf, int off, int len) {
final String message = new String(buf, off, len);
SwingUtilities.invokeLater(new Runnable(){
public void run(){
sb.append(message);
if(sb.length()>4000)sb.delete(0,3000);// 控制 sb 的大小确保不会占用太大内存。
sb.append("\n-------sb:"+sb.capacity()+"-------\n");
component.setText(sb.toString());
}
});
}
}
楼主【qzhl】截止到2008-07-08 08:24:21的历史汇总数据(不包括此帖):
发帖的总数量:3 发帖的总分数:70
结贴的总数量:2 结贴的总分数:50
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:20
结贴的百分比:66.67 % 结分的百分比:71.43 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主加油