package htmlconsole;
import java.awt.BorderLayout;
public class HTMLConsole extends JFrame {
private JPanel contentPane;
private JTextPane textPane;
public class Interceptor extends PrintStream
{
public Interceptor(OutputStream out)
{
super(out,true);
}
@Override
public void print(String s)
{
super.print(s);
textPane.setText(textPane.getText()+s);
}
@Override
public void println(String s)
{
super.println(s);
textPane.setText(textPane.getText()+"\n<br>");
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HTMLConsole frame = new HTMLConsole();
frame.setVisible(true);
System.out.println("Hello");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public HTMLConsole() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setContentType("text/html");
textPane.setText("");
contentPane.add(textPane, BorderLayout.CENTER);
//redirect stream
PrintStream interceptor = new Interceptor(System.out);
System.setOut(interceptor);
}
}
输出:
Hellojava.lang.NullPointerException
at console.Console$Interceptor.print(Console.java:29)
at java.io.PrintStream.println(Unknown Source)
at console.Console$Interceptor.println(Console.java:35)
at console.Console$1.run(Console.java:47)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我应该如何纠正这个程序?异常被抛出在未使用的“打印”方法中,这非常令人困惑!
更新:
替换JTextPane textPane = new JTextPane();
为后异常消失textPane = new JTextPane();
,但现在窗口内没有显示任何内容。