以下類使用JInternalFrame來存放顯示所有重定向的println和err語句的Textarea。在java中將log4j語句重定向到自定義控制檯
public class ConsoleFrame extends JInternalFrame
{
JTextArea outArea = new JTextArea(10,100);
static JInternalFrame cons;
public ConsoleFrame()
{
outArea.setLineWrap(true);
JScrollPane pain = new JScrollPane(outArea);
//pain.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.add(pain);
this.setVisible(true);
this.setSize(1000,400);
this.setTitle("Groovy Console");
this.closable = false;
this.maximizable = false;
this.isSelected = true;
this.resizable = false;
BasicInternalFrameUI ui = (BasicInternalFrameUI)this.getUI();
Component north = ui.getNorthPane();
MouseMotionListener[] actions =
(MouseMotionListener[])north.getListeners(MouseMotionListener.class);
for (int i = 0; i < actions.length; i++)
north.removeMouseMotionListener(actions[i]);
this.setFocusable(false);
//logger
System.setErr(new PrintStream(new JTextAreaOutputStream(outArea)));
System.setOut(new PrintStream(new JTextAreaOutputStream(outArea)));
setConsole(this);
}
static public JInternalFrame getConsole(){
return cons;
}
public void setConsole(JInternalFrame console){
cons = console;
}
public class JTextAreaOutputStream extends OutputStream {
JTextArea ta;
public JTextAreaOutputStream(JTextArea t) {
super();
ta = t;
}
public void write(int i) {
ta.append(Character.toString((char)i));
}
public void write(char[] buf, int off, int len) {
String s = new String(buf, off, len);
ta.append(s);
}
}
}
該類只重定向sysout和syserr語句。我應該在代碼中將記錄器語句重定向到textarea中進行哪些修改?
您使用哪種日誌框架?內置的Java一個? Log4J的? – 2011-04-13 07:32:53
我正在使用log4j。 – 2011-04-13 08:55:46
有沒有你創建兩個流而不是一個的原因? – 2011-04-13 17:26:17