System.out.println到JTextArea

时间:2013-02-05 11:39:24

标签: java swing user-interface jtextarea

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

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

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

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)。

问题在于:

目前正在使用invokeLater(),如文档中所述:

Causes runnable to have its run method called in the dispatch thread of the EventQueue. This will happen after all pending events are processed.

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

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

感谢,

5 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:4)

您的错误必须位于您尚未向我们展示的其他地方。这是一个非常简单的演示,按预期工作,与您的代码几乎相同(我只修复了一些小问题):

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
import javax.swing.Timer;

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";

            textArea.append(text);
            sb.setLength(0);
        } else {
            sb.append((char) b);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame(TextAreaOutputStream.class.getSimpleName());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JTextArea ta = new JTextArea(24, 80);
                System.setOut(new PrintStream(new TextAreaOutputStream(ta)));
                frame.add(new JScrollPane(ta));
                frame.pack();
                frame.setVisible(true);
                System.out.println("Textarea console initiated");
                Timer t = new Timer(1000, new ActionListener() {

                    int count = 1;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Outputting line " + count++ + " to the console. Working properly, no?");
                    }
                });
                t.start();
            }
        });
    }
}

答案 2 :(得分:2)

您可能需要使用PipedOutputStream ...请在此处查看问题的答案: How to redirect all console output to a Swing JTextArea/JTextPane with the right encoding?

基本上它的作用是,它将System.out重定向到一个缓冲区,程序可以从中读取用System.out打印的输出。这称为Piping

答案 3 :(得分:0)

如果要在文本区域中查看滚动效果,则可以将新文本放在开头,而不是附加输出。 例如:

HttpURLConnection con = (HttpURLConnection) (new URL(url[0]).openConnection());
con.setInstanceFollowRedirects(false);
con.connect();
int responseCode = con.getResponseCode();
String location = con.getHeaderField("Location");
textArea.setText(url[0] +"," +responseCode+"," +location+"\n"+textArea.getText()); //new text is prefixed to the existing text
textArea.update(textArea.getGraphics());

答案 4 :(得分:0)

我发现了一个非常简单的,虽然可能不是最快或最好的方法。然而,它非常简单,如果你像我一样懒惰,那么它的效果很好。

JTextArea output = new JTextArea();
PrintStream out = new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
    output.append(""+(char)(b & 0xFF));
}
});
System.setOut(out);
System.out.println("TEST");