zoukankan      html  css  js  c++  java
  • 将控制台信息重新导向到JTextArea

    package com.function;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    import com.ui.Tools_UiDataPanelJTextArea;
    
    /*
     * 将System.out.println() 控制台信息重新导向
     */
    public class Tools_DataRedirection extends OutputStream   {
        private final JTextArea destination;  
        public Tools_DataRedirection (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);  
        }  
    
    }
     
    package com.ui;
    
    import java.io.PrintStream;
    
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    import com.function.Tools_DataRedirection;
    /*
     * 数据显示文本域类
     */
    public class Tools_UiDataPanelJTextArea extends JTextArea {
        public Tools_UiDataPanelJTextArea() {
            // TODO Auto-generated constructor stub
            Tools_DataRedirection out = new Tools_DataRedirection (this);  
            System.setOut (new PrintStream (out));//设置输出重定向   
            System.setErr(new PrintStream(out));//将错误输出也重定向,用于e.pritnStackTrace 
    
            
        }
    
    }
     



  • 相关阅读:
    Golang理解-字符串拼接的几种方式
    Golang理解-匿名函数
    Golang理解-函数变量
    Golang理解-函数声明
    安全生产的规范和机制
    服务可用性指南
    c#中Class和Struct使用与性能的区别
    asp.net HTTP请求过程
    如何设置ASP.NET站点页面运行超时
    Nginx——在Windows环境下安装(一)
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708643.html
Copyright © 2011-2022 走看看