编程语言
首页 > 编程语言> > java – JTextArea在其他JFrame中显示实时控制台输出

java – JTextArea在其他JFrame中显示实时控制台输出

作者:互联网

Gui显示控制台打印,使用了重定向

private void redirectSystemStreams() {
    OutputStream out = new OutputStream() {
        @Override
        public void write(int b) throws IOException {
            updateTextArea(String.valueOf((char) b));
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            updateTextArea(new String(b, off, len));
        }

        @Override
        public void write(byte[] b) throws IOException {
            write(b, 0, b.length);
        }
    };

    System.setOut(new PrintStream(out, true));
    System.setErr(new PrintStream(out, true));
}
private void updateTextArea(final String text) {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                txt_console.append(text);
            }
        });
    } catch (InterruptedException ex) {
    } catch (InvocationTargetException ex) {
    }
}

本页面仅作备案,详细请看参考

参考:http://itpcb.com/a/362920

标签:JFrame,java,int,void,write,Override,new,实时控制,public
来源: https://www.cnblogs.com/2020hyc/p/15424556.html