相关疑难解决方法(0)

将System.out重定向到JTextPane

我有一个类(如下所示)扩展JPanel并包含一个JTextPane.我想重定向System.outSystem.errJTextPane.我的班级似乎没有用.当我运行它时,它确实重定向系统打印,但它们不打印到我的JTextPane.请帮忙!

注意:仅在应用程序启动时重定向调用.但是在发布后的任何时候,System.out呼叫都不会被重定向到JTextPane.(即,如果我System.out.prinln();在类中放置一个,它将被调用,但是如果它被放置在以actionListener供以后使用,则它不会重定向).

public class OSXConsole extends JPanel {
    public static final long serialVersionUID = 21362469L;

    private JTextPane textPane;
    private PipedOutputStream pipeOut;
    private PipedInputStream pipeIn;


    public OSXConsole() {
        super(new BorderLayout());
        textPane = new JTextPane();
        this.add(textPane, BorderLayout.CENTER);

        redirectSystemStreams();

        textPane.setBackground(Color.GRAY);
        textPane.setBorder(new EmptyBorder(5, 5, 5, 5));

    }


    private void updateTextPane(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Document doc = …
Run Code Online (Sandbox Code Playgroud)

java swing

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

swing ×1