求将System.out的打印信息重定向至JTextArea的思路

westwin 2005-05-12 09:25:12
入题所述,
...全文
357 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chongkai 2005-05-12
  • 打赏
  • 举报
回复
updn(快乐编程) 的办法比我的简单。自己写了个OutputStream,挺好。我本来想继承一个,但是访问不到它的buffer,所以才用了piped。

有点建议:
1 每次write之后直接flush,而不要等到buffer满了之后
2 OutputStream的几个write方法都应该覆盖
chongkai 2005-05-12
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.event.*;

class MyPipe implements Runnable{
private JTextArea textarea;
private PipedInputStream pis = new PipedInputStream();
private PipedOutputStream pos;
private BufferedReader reader = new BufferedReader(
new InputStreamReader(pis));
private Thread thread = null;

public MyPipe(JTextArea ta) throws IOException{
textarea = ta;
pos = new PipedOutputStream(pis);
thread = new Thread(this);
thread.start();
}

public PipedOutputStream getOut(){
return pos;
}

public void run(){
String line = null;
while(thread == Thread.currentThread()){
try{
line = reader.readLine();
}
catch(IOException ioe){
break;
}

if(line == null){
break;
}
else{
final String s = line;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
textarea.append(s);
textarea.append(System.getProperty("line.separator", "\n\r"));
}
});
}
}
}

public void close(){
thread = null;
}
}

public class Sink extends JFrame{
private MyPipe pipe;

public Sink() throws IOException{
JTextArea area = new JTextArea(10, 40);
area.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println(e.getX() + ", " + e.getY());
}
});

JScrollPane scroll = new JScrollPane(area);
pipe = new MyPipe(area);
System.setOut(new PrintStream(pipe.getOut(), true));
this.getContentPane().add(scroll, BorderLayout.CENTER);
this.pack();
this.setLocation(200, 200);
this.setVisible(true);
}

public void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID() == e.WINDOW_CLOSING){
pipe.close();
System.exit(0);
}
}

public static void main(String[] args)throws IOException{
Sink sink = new Sink();
}
}
updn 2005-05-12
  • 打赏
  • 举报
回复
PrintStream ps = new PrintStream(new OutputStream(){

final int LENGTH = 256;
byte[] bb = new byte[LENGTH];
int p = 0;
public void write(int b) throws IOException {
bb[p++] = (byte)b;

if(p >= LENGTH){
flush();
}
}

public void flush()throws IOException{
String str = new String(bb,0,p);

jTextArea1.append(str);

p = 0;
}
},true);

System.setOut(ps);

System.out.println("test str"};
westwin 2005-05-12
  • 打赏
  • 举报
回复
1楼的代码好像不起效果,不过看起来很正确
MARS.nEIL 2005-05-12
  • 打赏
  • 举报
回复
UP.学习.
bevin1010 2005-05-12
  • 打赏
  • 举报
回复
jtextOut = new OutputStream() {

final int BUFFER_LENGTH = 1024;
byte buf[] = new byte[BUFFER_LENGTH];
int pos = 0;
public void write(int b) throws IOException {
buf[pos ++] = (byte)b;
if (pos >= BUFFER_LENGTH)
flush();
}

public void flush() throws IOException
{
if (pos >= BUFFER_LENGTH)
txtLog.append(new String(buf));
else
txtLog.append(new String(buf, 0, pos));
pos = 0;
}
};
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
//output message to textarea
System.setOut(new PrintStream(jtextOut, true));
aid666 2005-05-12
  • 打赏
  • 举报
回复
不懂....
可不可以将Area的内容装到一个OutputStream中?之后用这个进行输出?
如何将一个OutputStream定为默认输出流,等待高手 。。。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧