JTextArea as console

前端 未结 1 1231
野的像风
野的像风 2020-12-06 07:22

I have posted two pieces of code below. Both codes work fine individually. Now, when I run the file Easy, and click on the \"Start\" button, I want the class AddNumber to be

1条回答
  •  北海茫月
    2020-12-06 08:18

    If you do a Google search for: "stdout JTextArea", you will a couple of links to solve your problem.

    • http://www.coderanch.com/t/458147/GUI/java/Redirect-output-stderr-stdout-JTextArea
    • Redirecting System.out to JTextPane
    • http://www.jcreator.com/forums/index.php?showtopic=773

    In the last link, buddybob extends java.io.OutputStream to print standard output to his JTextArea. I included his solution below.

    TextAreaOutputStream.java

    /*
    *
    * @(#) TextAreaOutputStream.java
    *
    */
    
    import java.io.IOException;
    import java.io.OutputStream;
    import javax.swing.JTextArea;
    
    /**
    * An output stream that writes its output to a javax.swing.JTextArea
    * control.
    *
    * @author  Ranganath Kini
    * @see      javax.swing.JTextArea
    */
    public class TextAreaOutputStream extends OutputStream {
        private JTextArea textControl;
    
        /**
         * Creates a new instance of TextAreaOutputStream which writes
         * to the specified instance of javax.swing.JTextArea control.
         *
         * @param control   A reference to the javax.swing.JTextArea
         *                  control to which the output must be redirected
         *                  to.
         */
        public TextAreaOutputStream( JTextArea control ) {
            textControl = control;
        }
    
        /**
         * Writes the specified byte as a character to the
         * javax.swing.JTextArea.
         *
         * @param   b   The byte to be written as character to the
         *              JTextArea.
         */
        public void write( int b ) throws IOException {
            // append the data as characters to the JTextArea control
            textControl.append( String.valueOf( ( char )b ) );
        }  
    }
    

    The TextAreaOutputStream extends the java.io.OutputStream class and overrides its write(int) method overload, this class uses a reference to a javax.swing.JTextArea control instance and then appends output to it whenever its write( int b ) method is called.

    To use the TextAreaOutputStream class, [yo]u should use:

    Usage

    // Create an instance of javax.swing.JTextArea control
    JTextArea txtConsole = new JTextArea();
    
    // Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
    // PrintStream around it to support the println/printf methods.
    PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) );
    
    // redirect standard output stream to the TextAreaOutputStream
    System.setOut( out );
    
    // redirect standard error stream to the TextAreaOutputStream
    System.setErr( out );
    
    // now test the mechanism
    System.out.println( "Hello World" );
    

    0 讨论(0)
提交回复
热议问题