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();
rectangle abounds = 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();
}
}
}
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();
rectangle abounds = 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();
}
}
}
闽公网安备 35060202000074号