简体   繁体   English

其他JFrame中的JTextArea显示实时控制台输出

[英]JTextArea in other JFrame display realtime console output

I have a "ConsoleFrame" which should display my console output in real-time to a JTextArea. 我有一个“ ConsoleFrame”,该控制台应将我的控制台输出实时显示到JTextArea。

I redirected the output streams: 我重定向了输出流:

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));
}

and call the SwingUtilities.invokeAndWait method to append the new text, which works fine 并调用SwingUtilities.invokeAndWait方法添加新文本, 效果很好

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) {
    }
}

but it shows me in my new ConsoleFrame this error: java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread and I get that because of the EDT - but why does it work and how can I adapt my code to make it work properly? 但是它在我的新ConsoleFrame中向我显示此错误: java.lang.Error:无法从事件调度程序线程中调用invokeAndWait ,由于EDT而得到该错误-但是它为什么起作用以及如何修改我的代码使其起作用好吗

  • invokeAndWait must be called out of EDT, otherwise caused am exceptions, 必须从EDT中调用invokeAndWait ,否则会导致异常,

  • carefully with invokeAndWait , because can freeze whole Swing GUI, locked by exceptions from RepaintManager (not in all cases only GUI is created, relayout, refreshed some of methods), then applications required restart, 请谨慎地使用invokeAndWait ,因为它可以冻结整个Swing GUI,并被RepaintManager的异常锁定(并非在所有情况下都只创建GUI,重新布局,刷新某些方法),然后需要重新启动应用程序,

  • for invokeAndWait is required to test if (EventQueue.isDispatchThread()) { / if (SwingUtilities.isEventDispatchThread()) { on true you can to setText("") / append("" ) without any side effects, output is done on EDT, but is about good practicies to wrap inside invokeLater 需要invokeAndWait测试if (EventQueue.isDispatchThread()) { / if (SwingUtilities.isEventDispatchThread()) {为true,则可以设置setText("") / append("" ),而不会产生任何副作用,美国东部时间(EDT),但有关将良好的实践包装到invokeLater

  • use SwingWorker , there are implemented methods process , publish , setProcess and done , all mentioned methods notified EDT by default , 使用SwingWorker ,有实现的方法processpublishsetProcessdoneEDT by default所有提到的方法都会通知EDT by default

  • SwingWorker is designated to run only once time, for repeatly (on some period) to use Executor for SwingWorker or Runnable#Thread as most simple, clear and without any side issues, effects SwingWorker被指定为仅运行一次,以便重复(在一段时间内)将Executor用于SwingWorkerRunnable#Thread是最简单,清晰且没有任何副作用,影响的

You can use SwingUtilities.invokeLater() from any thread, and for error stream it is likely best to use it even when you are already in EDT: 您可以从任何线程使用SwingUtilities.invokeLater() ,并且对于错误流,即使您已经在EDT中,也可能最好使用它:

private void updateTextArea(final String text) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            txt_console.append(text);
        }
    });
}

That particular error you get comes from outside the EDT, and then invokeAndWait() may be called so you get the output to your console. 您遇到的特定错误来自EDT外部,然后可能调用invokeAndWait()以便将输出输出到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM