Java 控制台 JPanel

发布于 2024-10-07 09:28:24 字数 58 浏览 0 评论 0原文

你好 是否可以在 JPanel 中绘制 java 控制台返回的内容? 你有教程可以遵循吗? 谢谢 SW

Hello
Is it possible to draw in a JPanel what the java console is returning ?
have you got a tutorial to follow ?
thanks
sw

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

潜移默化 2024-10-14 09:28:24

我不记得在哪里找到这个,但我已经使用一个我称为 TextAreaOutputStream 的类将输出流输出到 JPanel 中保存的 JTextArea:

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TextAreaOutputStream extends OutputStream {

    private final JTextArea textArea;
    private final StringBuilder sb = new StringBuilder();
    private String title;

    public TextAreaOutputStream(final JTextArea textArea, String title) {
        this.textArea = textArea;
        this.title = title;
        sb.append(title + "> ");
    }

    @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(title).append("> ");
        }

        sb.append((char) b);
    }
}

然后,我将标准输出流重定向到该对象,正如 Alex 在上面的回答中提到的那样。

I can't remember where I found this, but I have outputted the output stream to a JTextArea held in a JPanel using a class I call TextAreaOutputStream:

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TextAreaOutputStream extends OutputStream {

    private final JTextArea textArea;
    private final StringBuilder sb = new StringBuilder();
    private String title;

    public TextAreaOutputStream(final JTextArea textArea, String title) {
        this.textArea = textArea;
        this.title = title;
        sb.append(title + "> ");
    }

    @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(title).append("> ");
        }

        sb.append((char) b);
    }
}

I then re-direct the standard output Stream to this object as Alex mentions in his answer above.

随波逐流 2024-10-14 09:28:24

首先从控制台读取。为此,请使用 System.setOut()。使用 ByteOutputStream,在那里写入并从中读取。您将得到程序打印到系统的内容。现在使用 TextArea 或 JScrollPane 来显示文本。

First read from the console. To do this use System.setOut(). Use ByteOutputStream, write there and read from their. You will get what your program prints to it system out. Now use either TextArea or JScrollPane to present the text.

爱已欠费 2024-10-14 09:28:24

创建 FilterOutputStream 的子类以将所有内容回显到 JTextArea。

class Echo extends FilterOutputStream {

    private final JTextArea text;

    public Echo(OutputStream out, JTextArea text) {
        super(out);
        if (text == null) throw new IllegalArgumentException("null text");
        this.text = text;
    }

    @Override
    public void write(int b) throws IOException {
        super.write(b);
        text.append(Character.toString((char) b));
        // scroll to end?
    }

    // overwrite the other write methods for better performance
}

并替换标准输出:

    JTextArea text = new JTextArea();
    System.setOut(new PrintStream(new Echo(System.out, text)));

Create a subclass of FilterOutputStream to echo everything to a JTextArea.

class Echo extends FilterOutputStream {

    private final JTextArea text;

    public Echo(OutputStream out, JTextArea text) {
        super(out);
        if (text == null) throw new IllegalArgumentException("null text");
        this.text = text;
    }

    @Override
    public void write(int b) throws IOException {
        super.write(b);
        text.append(Character.toString((char) b));
        // scroll to end?
    }

    // overwrite the other write methods for better performance
}

and replace the standard output:

    JTextArea text = new JTextArea();
    System.setOut(new PrintStream(new Echo(System.out, text)));
找回味觉 2024-10-14 09:28:24

消息控制台提供了更多您可能感兴趣的选项。

The Message Console provides a few more options that might interest you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文