Redirect Output (stdoutstderr) to a frame

zhaozj2021-02-08  340

Import java.awt. *;

Import java.awt.event. *;

Import java.io. *;

public

Class RedirectedFrame

Extends frame {

Textarea atextarea =

NEW textarea ();

PrintStream AprintStream =

New PrintStream

New filteredstream

New byteArrayoutputStream ()));

Boolean logfile;

RedirectedFrame

Boolean logfile) {

THIS.LOGFILE = Logfile;

System.setout (AprintStream);

System.seterr (AprintStream);

Settitle

"Error Message";

Setsize (500, 300);

setlayout

New borderLayout ());

Add (

"Center", atextarea;

DisplayLog ();

AddWindowListener

(

New windowadapter () {

public

Void windowClosing (WindowEvent E) {

Dispose ();

}

}

);

}

Class FilteredStream

EXTENDS FILTEROUTPUTSTREAM {

Public FilteredStream (OutputStream Astream) {

Super (astream);

}

public

Void Write

Byte B [])

THROWS IOEXCEPTION {

String astring =

NEW STRING (B);

Atextarea.Append (astring);

}

public

Void Write

Byte B [],

Int off,

int LEN

THROWS IOEXCEPTION {

String astring =

New String (B, OFF, LEN);

Atextarea.Append (astring);

IF (logfile) {

FileWriter awriter =

New FileWriter

"Error.log",

True);

AWriter.write (astring);

awriter.close ();

}

}

}

public

Void DisplayLog () {

Dimension Dim = GetToolkit (). GetScreensize ();

Rectangleworks = getBounds ();

Dimension DD = getSize ();

SetLocation (Dim.Width - ABOUNDS.WIDTH) / 2,

(Dim.height - ABOUNDS.HEIGHT / 2);

Setvisible

True);

Requestfocus ();

}

public

Static

void main (string s []) {

Try {

// force an exception for Demonstration Purpose

Class.Forname

"unknown"). Newinstance ();

}

Catch (Exception E) {

// for applet, always redirectedframe (false)

RedirectedFrame R =

New redirectedframe

True);

E.PrintStackTrace ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-864.html

New Post(0)