输出重定向

  1. package com.unmi;   
  2.   
  3. import java.awt.*;   
  4. import java.io.*;   
  5.   
  6. import javax.swing.*;   
  7. import javax.swing.text.*;   
  8.   
  9. /**  
  10.  * @author Unmi  
  11.  */  
  12. public class ConsolePane extends JScrollPane {   
  13.   
  14.     private JTextPane textPane = new JTextPane();   
  15.   
  16.     private static ConsolePane console = null;   
  17.   
  18.     public static synchronized ConsolePane getInstance() {   
  19.         if (console == null) {   
  20.             console = new ConsolePane();   
  21.         }   
  22.         return console;   
  23.     }   
  24.   
  25.     private ConsolePane() {   
  26.   
  27.         setViewportView(textPane);   
  28.   
  29.         // Set up System.out   
  30.         PrintStream mySystemOut = new MyPrintStream(System.out, Color.BLACK);   
  31.         System.setOut(mySystemOut);   
  32.   
  33.         // Set up System.err   
  34.         PrintStream mySystemErr = new MyPrintStream(System.err, Color.RED);   
  35.         System.setErr(mySystemErr);   
  36.            
  37.         textPane.setEditable(true);   
  38.         setPreferredSize(new Dimension(640120));   
  39.     }   
  40.   
  41.     /**  
  42.      * Returns the number of lines in the document.  
  43.      */  
  44.     private final int getLineCount() {   
  45.         return textPane.getDocument().getDefaultRootElement().getElementCount();   
  46.     }   
  47.   
  48.     /**  
  49.      * Returns the start offset of the specified line.  
  50.      * @param line  The line  
  51.      * @return The start offset of the specified line, or -1 if the line is  
  52.      *         invalid  
  53.      */  
  54.     private int getLineStartOffset(int line) {   
  55.         Element lineElement = textPane.getDocument().getDefaultRootElement()   
  56.                 .getElement(line);   
  57.         if (lineElement == null)   
  58.             return -1;   
  59.         else  
  60.             return lineElement.getStartOffset();   
  61.     }   
  62.   
  63.     /**  
  64.      * 清除超过行数时前面多出行的字符  
  65.      */  
  66.     private void replaceRange(String str, int start, int end) {   
  67.         if (end < start) {   
  68.             throw new IllegalArgumentException("end before start");   
  69.         }   
  70.         Document doc = textPane.getDocument();   
  71.         if (doc != null) {   
  72.             try {   
  73.                 if (doc instanceof AbstractDocument) {   
  74.                     ((AbstractDocument) doc).replace(start, end - start, str,   
  75.                             null);   
  76.                 } else {   
  77.                     doc.remove(start, end - start);   
  78.                     doc.insertString(start, str, null);   
  79.                 }   
  80.             } catch (BadLocationException e) {   
  81.                 throw new IllegalArgumentException(e.getMessage());   
  82.             }   
  83.         }   
  84.     }   
  85.   
  86.     class MyPrintStream extends PrintStream {   
  87.   
  88.         private Color foreground; //输出时所用字体颜色   
  89.            
  90.         /**  
  91.          * 构造自己的 PrintStream  
  92.          * @param out 可传入 System.out 或 System.err, 实际不起作用  
  93.          * @param foreground 显示字体颜色  
  94.          */  
  95.         MyPrintStream(OutputStream out,Color foreground) {   
  96.             super(out,true); //使用自动刷新   
  97.             this.foreground = foreground;   
  98.         }   
  99.   
  100.         /**  
  101.          * 在这里重截,所有的打印方法都要调用最底一层的方法  
  102.          */  
  103.         public void write(byte[] buf, int off, int len) {   
  104.             final String message = new String(buf, off, len);   
  105.   
  106.             /** SWING非界面线程访问组件的方式 */  
  107.             SwingUtilities.invokeLater(new Runnable() {   
  108.                 public void run() {   
  109.                     try {   
  110.   
  111.                         StyledDocument doc = (StyledDocument) textPane   
  112.                                 .getDocument();   
  113.   
  114.                         // Create a style object and then set the style   
  115.                         // attributes   
  116.                         Style style = doc.addStyle("StyleName"null);   
  117.   
  118.                         // Foreground color   
  119.                         StyleConstants.setForeground(style, foreground);   
  120.   
  121.                         doc.insertString(doc.getLength(), message, style);   
  122.   
  123.                     } catch (BadLocationException e) {   
  124.                         // e.printStackTrace();   
  125.                     }   
  126.   
  127.                     // Make sure the last line is always visible   
  128.                     textPane.setCaretPosition(textPane.getDocument()   
  129.                             .getLength());   
  130.   
  131.                     // Keep the text area down to a certain line count   
  132.                     int idealLine = 150;   
  133.                     int maxExcess = 50;   
  134.   
  135.                     int excess = getLineCount() - idealLine;   
  136.                     if (excess >= maxExcess) {   
  137.                         replaceRange(""0, getLineStartOffset(excess));   
  138.                     }   
  139.                 }   
  140.             });   
  141.         }   
  142.     }   
  143. }   

使用方法依旧,是一个 JScrollPane,加上自己的面板上就行:
getContentPane().add(ConsolePane.getInstance(), BorderLayout.CENTER);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值