如何将输出流设置为 TextArea [英] How to set output stream to TextArea

查看:29
本文介绍了如何将输出流设置为 TextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个程序创建一个 GUI 面板,我想要一切,通常会打印到我的命令提示符,打印到 TextArea 对象.我的 GUI 面板大部分都格式化了,但我无法将文本打印到 TextArea,这是我的文件:

I'm trying to create a GUI Panel for a program and I'd like everything, that would normally print to my command prompt, to print to a TextArea object. I have the GUI panel formatted for the most part, I can't get the text to print to the TextArea though, here's my file:

package guipanel;

import javax.swing.*;
import java.awt.*;
import java.io.*;


/**
 *
 * @author Dan
 */
public class GUIPanel extends JFrame { 
    public GUIPanel() {
        initComponents();
    }
    private void setOutputStream(boolean catchErrors) {
        System.setOut(aPrintStream); 
        setVisible(true);
        requestFocus();
        if (catchErrors) {
               System.setErr(aPrintStream);
        }
    }
    private void addTabs(JTabbedPane jTabbedPane1) {
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        JPanel jPanel3 = new JPanel();
        JPanel jPanel4 = new JPanel();
        jTabbedPane1.add("Main", textArea1);
        jTabbedPane1.add("Commands", jPanel);
        jTabbedPane1.add("Rules", jPanel1);
        jTabbedPane1.add("Links", jPanel2);
        jTabbedPane1.add("Information", jPanel3);
        jTabbedPane1.add("Shutdown", jPanel4);
        setOutputStream(true);
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {

        textArea1 = new java.awt.TextArea();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        textArea1.setPreferredSize(new java.awt.Dimension(432, 343));
        textArea1.getAccessibleContext().setAccessibleParent(jTabbedPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Evolution-X 639");
        setBounds(new java.awt.Rectangle(0, 0, 400, 450));
        setResizable(false);
        getContentPane().setLayout(new java.awt.FlowLayout());

        addTabs(jTabbedPane1);
        getContentPane().add(jTabbedPane1);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPanel().setVisible(true);
            }
        });
    }
    private JMenu jMenu1;
    private JMenu jMenu2;
    private JMenuBar jMenuBar1;
    private JTabbedPane jTabbedPane1;
    private TextArea textArea1;
    private JPanel jPanel = new JPanel();
    private PrintStream aPrintStream  =
       new PrintStream(
         new FilterOutputStream(
           new ByteArrayOutputStream()));
}

推荐答案

您需要将打印流重定向到您可以控制的输出流...

You need to redirect the print stream to an output stream you can control...

这是我为正在工作的应用程序开发的概念示例.当它在用户站点上运行时,我们使用它来调出输出控制台,以便我们可以看到发送到标准输出的内容......直到我们修复了我们的日志记录;)

This is an example of concept I developed for an application I'm working on for work. We use this to bring up the output console when it's running at user sites so we can see what's being sent to standard out...until we fixed our logging that is ;)

基本上,它在打印流和控制台之间放置了一个自定义 OutputStream 以捕获输出,但仍允许将内容打印到控制台.如果您从命令行或 IDE 运行程序,这会很有帮助.如果你愿意,你可以设置一个开关来阻止这个......

Basically it puts a custom OutputStream in between the print stream and the console to capture the output, but still allows the content to printed to the console. This is helpful if you're running the program from the command line or IDE. You could put a switch to stop this if you wanted...

public class TestRedirect {

    public static void main(String[] args) {
        new TestRedirect();
    }

    public TestRedirect() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                CapturePane capturePane = new CapturePane();
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(capturePane);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                PrintStream ps = System.out;
                System.setOut(new PrintStream(new StreamCapturer("STDOUT", capturePane, ps)));

                System.out.println("Hello, this is a test");
                System.out.println("Wave if you can see me");
            }            
        });
    }

    public class CapturePane extends JPanel implements Consumer {

        private JTextArea output;

        public CapturePane() {
            setLayout(new BorderLayout());
            output = new JTextArea();
            add(new JScrollPane(output));
        }

        @Override
        public void appendText(final String text) {
            if (EventQueue.isDispatchThread()) {
                output.append(text);
                output.setCaretPosition(output.getText().length());
            } else {

                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        appendText(text);
                    }
                });

            }
        }        
    }

    public interface Consumer {        
        public void appendText(String text);        
    }

    public class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private String prefix;
        private Consumer consumer;
        private PrintStream old;

        public StreamCapturer(String prefix, Consumer consumer, PrintStream old) {
            this.prefix = prefix;
            buffer = new StringBuilder(128);
            buffer.append("[").append(prefix).append("] ");
            this.old = old;
            this.consumer = consumer;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("
")) {
                consumer.appendText(buffer.toString());
                buffer.delete(0, buffer.length());
                buffer.append("[").append(prefix).append("] ");
            }
            old.print(c);
        }        
    }    
}

更新了工作示例.在 Windows 7、Java 6 和 Mac OS Lion Java 7 上测试

Updated with working example. Test on Windows 7, Java 6 and Mac OS Lion Java 7

这篇关于如何将输出流设置为 TextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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