System.out.println到JTextArea [英] System.out.println to JTextArea

查看:115
本文介绍了System.out.println到JTextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我已编辑帖子以澄清我的问题,现在我自己也有更多的理解。

I have edited the post to clarify my question, now I myself, have more understanding.

我基本上是标题说,尝试在我的GUI中输出控制台到我的 JTextArea ,同时执行应用程序的任务。

I am essentially, as the title says, attempting to output console to my JTextArea in my GUI, whilst performing the tasks of the application.

以下是我目前正在做的事情:

Here is what I am currently doing:

public class TextAreaOutputStream extends OutputStream
{

    private final JTextArea textArea;

    private final StringBuilder sb = new StringBuilder();

    public TextAreaOutputStream(final JTextArea textArea)
    {
        this.textArea = textArea;
    }

    @Override
    public void flush()
    {
    }

    @Override
    public void close()
    {
    }

    @Override
    public void write(int b) throws IOException
    {

        if (b == '\r')
            return;

        if (b == '\n')
        {
            final String text = sb.toString() + "\n";
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    textArea.append(text);
                }
            });
            sb.setLength(0);
        }
        sb.append((char) b);
    }
}

上述内容将成功重定向 System.out 到上面的输出流,因此将事件发送到 EventQueue 来更新我的GUI( JTextArea )。

The above will successfully re-direct System.out to my output stream above and therefore despatch an event to the EventQueue to update my GUI (JTextArea).

以下是问题:

目前使用 invokeLater()将如文档中所述:

Currently using invokeLater() will as it says on the docs:

导致runnable调用其run方法EventQueue的调度线程。这将在处理完所有待处理事件后发生。

所以我真正想做的是执行我对GUI的更新(调用 run())在处理EventQueue中的其他所有内容之前。

So what I actually want to do is perform my update to the GUI (call run()) before processing everything else in the EventQueue.

是否可以将一个事件注入我的EventQueue?或者有人可以指点我在这个领域的一个体面的教程?

Is it possible to inject an event essentially into my EventQueue? Or can somebody point me to an decent tutorial on this area?

谢谢,

推荐答案

以下示例使用文本区域创建框架并将System.out重定向到它:

The following example creates frame with text area and redirects System.out to it:

import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class JTextAreaOutputStream extends OutputStream
{
    private final JTextArea destination;

    public JTextAreaOutputStream (JTextArea destination)
    {
        if (destination == null)
            throw new IllegalArgumentException ("Destination is null");

        this.destination = destination;
    }

    @Override
    public void write(byte[] buffer, int offset, int length) throws IOException
    {
        final String text = new String (buffer, offset, length);
        SwingUtilities.invokeLater(new Runnable ()
            {
                @Override
                public void run() 
                {
                    destination.append (text);
                }
            });
    }

    @Override
    public void write(int b) throws IOException
    {
        write (new byte [] {(byte)b}, 0, 1);
    }

    public static void main (String[] args) throws Exception
    {
        JTextArea textArea = new JTextArea (25, 80);

        textArea.setEditable (false);

        JFrame frame = new JFrame ("stdout");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane ();
        contentPane.setLayout (new BorderLayout ());
        contentPane.add (
            new JScrollPane (
                textArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
            BorderLayout.CENTER);
        frame.pack ();
        frame.setVisible (true);

        JTextAreaOutputStream out = new JTextAreaOutputStream (textArea);
        System.setOut (new PrintStream (out));

        while (true)
        {
            System.out.println ("Current time: " + System.currentTimeMillis ());
            Thread.sleep (1000L);
        }
    }
}

这篇关于System.out.println到JTextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆