今天在csdn上看到一帖提问,想把控制台的输出信息转移到JTextArea中,一开始觉得好像没什么好的办法。但发现一位kokonol网友的实现想法很有创意,不禁觉得自己真是脑筋转不过来,特意记录下来:
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test extends JFrame {
public static JTextArea text; //输出JTextArea
private static class System //覆盖System.out.pringln()
{
private static class out
{
private static void println(String a)
{
text.append(a);
}
}
}
public static void main(String args[]) {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Test() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JTextArea();
getContentPane().add(text, BorderLayout.CENTER);
//测试按钮
final JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
System.out.println("OK");
}
});
button.setText("New JButton");
getContentPane().add(button, BorderLayout.SOUTH);
}
}
==================================================
主要就是这里了,覆盖一下System.out.println()
不过JTextArea必须声明为static的
public static JTextArea text; //输出JTextArea
private static class System //覆盖System.out.pringln()
{
private static class out
{
private static void println(String a)
{
text.append(a);
}
}
}
==============================================
楼下又有一位网友继续补充完善:
Kokonol的方法应该说很有创意,但截获在其他类里的输出就有问题了
标准的方法是调用
System.setOut(PrintStream out)
System.setErr(PrintStream err)
即提供一个自定义的PrintStream,将一个JTextArea作为PrintStream的输出目的地。
基本思路(也可考虑使用PipedInputStream,PipedOutputStream ):
OutputStream textAreaStream = new OutputStream() {
public void write(int b) throws IOException {
text.append(String.valueOf((char)b));
}
public void write(byte b[]) throws IOException {
text.append(new String(b));
}
public void write(byte b[], int off, int len) throws IOException {
text.append(new String(b, off, len));
}
};
PrintStream myOut = new PrintStream(textAreaStream);
System.setOut(myOut);
System.setErr(myOut);
借用一下上面的示例代码:
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.io.*;
public class Test extends JFrame {
public static JTextArea text; //输出JTextArea
public static void main(String args[]) {
OutputStream textAreaStream = new OutputStream() {
public void write(int b) throws IOException {
text.append(String.valueOf((char)b));
}
public void write(byte b[]) throws IOException {
text.append(new String(b));
}
public void write(byte b[], int off, int len) throws IOException {
text.append(new String(b, off, len));
}
};
PrintStream myOut = new PrintStream(textAreaStream);
System.setOut(myOut);
System.setErr(myOut);
try {
Test frame = new Test ();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Test () {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JTextArea();
getContentPane().add(text, BorderLayout.CENTER);
//测试按钮
final JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
System.out.println("OK");
System.out.println("测试按钮");
}
});
button.setText("New JButton");
getContentPane().add(button, BorderLayout.SOUTH);
}
}