posts - 58,  comments - 8,  trackbacks - 0
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.io.PrintStream;
class StreamToTextArea extends JFrame {
  //declare PrintStream and JTextArea
    private static PrintStream ps = null;
    private JTextArea textPane = new JTextArea();  //constructor
    public StreamToTextArea() {

    setSize( 310, 180 );

      getContentPane().add(textPane);

      //this is the trick: overload the println(String)
      //method of the PrintStream
      //and redirect anything sent to this to the text box
    ps = new PrintStream(System.out) {
      public void println(String x) {
        textPane.append(x + "\n");
      }
    };
    }

    public PrintStream getPs() {
      return ps;
    }

  public static void main(String args[]) {
    //create object
    StreamToTextArea blah = new StreamToTextArea();
    //show it
    blah.show();
    //redirect the output stream
    System.setOut(blah.getPs());
    //print to the text box
    System.out.println("IT'S ALIVE!!");
    //print to the terminal (not a string)
    System.out.println(1);
    //print the same thing to the text box (now a string)
    System.out.println("" + 1);
  }
}

// There are still many other ways to do same thing
posted on 2007-10-18 10:25 forrest 阅读(1669) 评论(0)  编辑 收藏 引用 所属分类: JAVA
<2008年2月>
272829303112
3456789
10111213141516
17181920212223
2425262728291
2345678

常用链接

留言簿(5)

随笔分类(59)

随笔档案(58)

文章分类(41)

文章档案(52)

相册

收藏夹

postfix

windows 系统

编程

  • how to be a programmer
  • 写给想当程序员的朋友
  • 谨以此文献给所有想当程序员的朋友 (一) 文章由来及个人经历 我是一名计算机专业的本科毕业生,毕业已经1年多了。毕业后从事的是软件编程工作,经常有其他专业的朋友想从事软件编程工作,向我请教如何,因为我自觉涉行不深,不敢信口开河,无奈朋友信任,我不得不郑重考虑一下这个问题了,来帮助朋友选择和回报朋友的信任。

搜索

  •  

最新评论

阅读排行榜

评论排行榜