(高分100)如何在application中实现把本来输出到控制台上的信息输出到容器(frame)中textarea界面上!(急等)

zhangsq 2003-03-27 06:54:07
如何在application中实现把本来输出到控制台上的信息输出到容器(frame)中textarea界面上!(急等)
...全文
564 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
oscarjiao 2003-03-29
  • 打赏
  • 举报
回复
用jtextarea里的函数settext(String);
muymuy 2003-03-29
  • 打赏
  • 举报
回复

这是一个简单的重定向标准输出的swing程序,用一个按钮来进行标准输出的切换,已经调试通过。
程序大部分是jbuilder生成,所以变量有点... ...

源程序如下:

//Frame1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Frame1 extends JFrame implements ActionListener
{
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton1 = new JButton();
JPanel jPanel1 = new JPanel();

PrintStream orignalOut;
OutputStream jtextOut;

//Construct the frame
public Frame1() {

//保存原始标准输出流
orignalOut = System.out;
//创建JTextArea输出流,用于重定向
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)
jTextArea1.append(new String(buf));
else
jTextArea1.append(new String(buf, 0, pos));

pos = 0;
}
};

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton1.setActionCommand("write to JTextArea");
jButton1.setText("write to JTextArea");
jButton1.addActionListener(this);
this.getContentPane().add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jButton1, null);
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
}

//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

boolean writeToConsole = true;
//按钮处理函数
public void actionPerformed(ActionEvent e)
{
if (e.getSource ()== this.jButton1)
{
if (writeToConsole)
{
// 将标准输出重定向到JTextArea
jButton1.setText("write to console");
// true表示自动调用flush
System.setOut(new PrintStream(jtextOut, true));
writeToConsole = false;
}
else
{
// 将标准输出恢复到原来
jButton1.setText("write to JTextArea");
System.setOut(orignalOut);
writeToConsole = true;
}
}
}

public static void main(String args[])
{
Frame1 frame = new Frame1();
frame.setVisible(true);

//下面这个线程每个0.5秒向标准输出打印一个字符串
new Thread (){
public void run()
{
while(true)
{
System.out.println("asdfasdf");
try{
Thread.sleep(500);
}catch(Exception e){}
}
}
}.start();

}
}
teaky2002 2003-03-28
  • 打赏
  • 举报
回复
StringWriter out
= new StringWriter(new OutputStreamWriter(System.out));
String outstr=out.toString();
txtarea.setText(outstr);
haoyoa 2003-03-28
  • 打赏
  • 举报
回复
FYI:
http://www.rgagnon.com/javadetails/java-0028.html
zhangsq 2003-03-28
  • 打赏
  • 举报
回复
bsd(小红帽菜鸟) :
您好!能否详细一点,最好给个简单的例子,
谢谢!
bsd 2003-03-28
  • 打赏
  • 举报
回复
自己写一个扩展OutputStream的类MyOutputStream,override它的几个方法
把它的几个write方法写成向你的JTextArea输出
然后
System.setOut(new PrintSteam(new MyOutputStream()));
即可
skysaint 2003-03-28
  • 打赏
  • 举报
回复
一开始也理解为直接向 textarea 放值呢。
楼上说了。没试过。不过为什么用System.exit(0)呢?
wes109 2003-03-28
  • 打赏
  • 举报
回复
强!
bjzhanghao 2003-03-28
  • 打赏
  • 举报
回复
流重定向演示应用程序展示了将全部三种标准流重定向到常规文件(或反之)的各个步骤。

应用程序假定已存在一个名为 Redirect.in 的文件。它代替 System.in 标准输入流读取文件,并将其打印到原始的 System.out 标准输出流上。

应用程序会分别将输出流 System.out 和标准错误流 System.err 重定向到 Redirect.out 和 Redirect.err 文件。

有了这些知识,我们现在就可以运用自如,自己编写 InputStream 和 PrintStream 的子类,在其中添加各种有趣的行为 -- 比如,将数据同时打印到原始控制台和文件,或者传送到一个套接字连接,等等...

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Copyright (c) John D. Mitchell, 1996 -- All Rights Reserved

PROJECT: JavaWorld
MODULE: Tips & Tricks
FILE: Redirect.java

AUTHOR: John D. Mitchell, Jul 31, 1996

REVISION HISTORY:
Name Date Description
---- ---- -----------
JDM 96.07.31 Initial version.

DESCRIPTION:
This file highlights the ability to redirect the standard
input, standard output, and standard error streams.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

import java.io.*;


public class Redirect
{
public static void main (String args[])
{
// Save the current standard input, output, and error streams
// for later restoration.
InputStream origIn = System.in;
PrintStream origOut = System.out;
PrintStream origErr = System.err;

// Create a new input stream from a file.
InputStream stdin = null;
try
{
stdin = new FileInputStream ("Redirect.in");
}
catch (Exception e)
{
// Sigh. Couldn't open the file.
System.out.println ("Redirect: Unable to open input file!");
System.exit (1);
}

// Create a new output stream for the standard output.
PrintStream stdout = null;
try
{
stdout = new PrintStream (new FileOutputStream
("Redirect.out"));
}
catch (Exception e)
{
// Sigh. Couldn't open the file.
System.out.println ("Redirect: Unable to open output file!");
System.exit (1);
}

// Create new output stream for the standard error output.
PrintStream stderr = null;
try
{
stderr = new PrintStream (new FileOutputStream
("Redirect.err"));
}
catch (Exception e)
{
// Sigh. Couldn't open the file.
System.out.println ("Redirect: Unable to open error file!");
System.exit (1);
}

// Print stuff to the original output and error streams.
// On most systems all of this will end up on your console when you
// run this application.
origOut.println ("\nRedirect: Round #1");
System.out.println ("Test output via 'System.out'.");
origOut.println ("Test output via 'origOut' reference.");
System.err.println ("Test output via 'System.err'.");
origErr.println ("Test output via 'origErr' reference.");

// Set the System out and err streams to use our replacements.
System.in = stdin;
System.out = stdout;
System.err = stderr;

// Print stuff to the original output and error streams.
// The stuff printed through the 'origOut' and 'origErr' references
// should go to the console on most systems while the messages
// printed through the 'System.out' and 'System.err' will end up in
// the files we created for them.
origOut.println ("\nRedirect: Round #2");
System.out.println ("Test output via 'System.out'.");
origOut.println ("Test output via 'origOut' reference.");
System.err.println ("Test output via 'System.err'.");
origErr.println ("Test output via 'origErr' reference.");

// Read some input and dump it to the console.
origOut.println ("\nRedirect: Round #3");
int inChar = 0;
while (-1 != inChar)
{
try
{
inChar = System.in.read();
}
catch (Exception e)
{
// Clean up the output and bail.
origOut.print ("\n");
break;
}
origOut.write (inChar);
}

// Close the streams.
try
{
stdin.close ();
stdout.close ();
stderr.close ();
}
catch (Exception e)
{
origOut.println ("Redirect: Unable to close files!");
System.exit (1);
}

System.exit (0);
}
}
javafounder 2003-03-28
  • 打赏
  • 举报
回复
必须重定向输出流,然后才能从能出数据流中获取输出信息显示到文本域里
mercury1231 2003-03-28
  • 打赏
  • 举报
回复
呵呵,楼上几位等于没说。

好像可以用重新定向的方法,你看一下System里边那几个类吧。
Mars_wx 2003-03-27
  • 打赏
  • 举报
回复
public YourClass extends JFrame
{
public YourClass()
{
JTextArea textArea = new JTextArea();
getContentPane().add(textArea, BorderLayout.SOUTH);
textArea.setText("Your String");
}
public static void main(String[] args)
{
new YourClass().setVisible(true);
}
}
zihuilegend 2003-03-27
  • 打赏
  • 举报
回复
textarea控件用其的方法是可以实现这个功能的,
其中一个方法是settext(string value);这个方法是设置控件的内容,擦掉以前的内容
另一个方法是append(string value);这个方法是在控件中已经存在的内容后面追加新的内容
这样就可以了.

62,635

社区成员

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

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