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



  • 相关阅读:
    php数组函数-array_push()
    php数组函数-array_pop()
    php数组函数-array_pad()
    php数组函数-array_merge()
    php数组函数-array_map()
    php数组函数-array_keys()
    php数组函数-array_key_exists()
    php数组函数-array_intersect()
    php数组函数-array_flip()
    php数组函数-array_filter()
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708643.html
Copyright © 2011-2022 走看看