2014-02-05 56 views

回答

0

這是一個非常不完整的答案,但它是一個開始。此代碼用於擴展javax.swing.JFrame的Test.java。它建立一個簡單的GUI,創建一個新的PrintStream和OutputStream(它覆蓋寫入(INT B)附加一個字符的JTextArea),使用System.setOut(PrintStream的),然後的System.out.println(字符串)來測試它。

import java.io.PrintStream; 
import java.io.OutputStream; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 

/** 
* 
* @author caucow 
*/ 
public class Test extends JFrame { 

    PrintStream printOut; 
    OutputStream out; 
    JPanel container; 
    JTextArea example; 

    public static void main(String[] args) { 
     Test test = new Test(); 
     test.setVisible(true); 
    } 

    public Test() { 
     setSize(500, 500); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Create the JTextArea with some default text. 
     example = new JTextArea("//There is some text here\n"); 

     example.setBounds(0, 0, 500, 500); 
     add(example); 

     // Create the OutputStream which will be written to when using System.out 
     out = new OutputStream() { 
      @Override 
      // Override the write(int) method to append the character to the JTextArea 
      public void write(int b) throws IOException { 
       example.append("" + (char) b); 
      } 
     }; 

     // Create the PrintStream that System.out will be set to. 
     // Make sure autoflush is true. 
     printOut = new PrintStream(out, true); 

     // Set the system output to the PrintStream 
     System.setOut(printOut); 

     // Test it out 
     System.out.println("\nTest Output"); 
     System.out.println(); 
     System.out.println("More testing."); 
     System.out.println("Line 4"); 
     System.out.println("Bob says hi."); 
    } 
} 

對於全控制檯功能,您可以覆蓋JTextAreas文本處理方法,並添加鍵/鼠聽衆的setEnabled(布爾)只要選擇開始之前或在JTextArea中的最後一行後,並執行按下回車鍵時的命令。

希望這有助於現在。 (我會回來的,後來編輯此,使它不那麼難看,等等)