现在要给以前的程序加上GUI界面,原来程序中的控制台输出信息,怎么把这些信息转到JTextArea中呢?不希望到处寻找System.out.println()进行修改。

解决方案 »

  1.   

    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);
    }}
      

  2.   

    主要就是这里了,覆盖一下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);
             }
        }
        }
      

  3.   

    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);
    }}
      

  4.   

    Kokonol()  好会想啊
         这样也搞定了
      

  5.   

    Kokonol,cheng_young(古道西风瘦马)都很有想法,顶
      

  6.   

    Java 5.0中好象有专门针对控制台输出的类,记不清是哪个类了,楼主再查查看