gpt4 book ai didi

java - System.out 重定向到 JTextPane

转载 作者:行者123 更新时间:2023-11-29 10:16:08 24 4
gpt4 key购买 nike

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 textPane = new JTextPane(); 替换为 textPane = new JTextPane(); 后异常消失,但现在窗口内没有显示任何内容。 enter image description here

最佳答案

你的构造函数调用

JTextPane textPane = new JTextPane(); // local variable declaration and initialization

而不是调用

this.textPane = new JTextPane()

因此拦截器使用的 textPane 字段为空。

另请注意,如堆栈跟踪所示,super.println() 调用 print()。这将导致传递给 println() 的每个字符串都被附加到文本 Pane 两次。

关于java - System.out 重定向到 JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17630848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com