package com.function;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import com.ui.Tools_UiDataPanelJTextArea;
/*
* 将System.out.println() 控制台信息重新导向
*/
public class Tools_DataRedirection extends OutputStream {
private final JTextArea destination;
public Tools_DataRedirection (JTextArea destination)
{
if (destination == null)
throw new IllegalArgumentException ("Destination is null");
this.destination = destination;
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException
{
final String text = new String (buffer, offset, length);
SwingUtilities.invokeLater(new Runnable ()
{
@Override
public void run()
{
destination.append (text);
}
});
}
@Override
public void write(int b) throws IOException
{
write (new byte [] {(byte)b}, 0, 1);
}
}
package com.ui;
import java.io.PrintStream;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.function.Tools_DataRedirection;
/*
* 数据显示文本域类
*/
public class Tools_UiDataPanelJTextArea extends JTextArea {
public Tools_UiDataPanelJTextArea() {
// TODO Auto-generated constructor stub
Tools_DataRedirection out = new Tools_DataRedirection (this);
System.setOut (new PrintStream (out));//设置输出重定向
System.setErr(new PrintStream(out));//将错误输出也重定向,用于e.pritnStackTrace
}
}