Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
s、 您将得到该异常,因为流的一端总是在另一端之前关闭。忽略这一例外情况应该是安全的。 import java.io.*; import java.util.Arrays; import javax.swing.*; public class Stand_Java_Swing - Fatal编程技术网

s、 您将得到该异常,因为流的一端总是在另一端之前关闭。忽略这一例外情况应该是安全的。 import java.io.*; import java.util.Arrays; import javax.swing.*; public class Stand

s、 您将得到该异常,因为流的一端总是在另一端之前关闭。忽略这一例外情况应该是安全的。 import java.io.*; import java.util.Arrays; import javax.swing.*; public class Stand,java,swing,Java,Swing,s、 您将得到该异常,因为流的一端总是在另一端之前关闭。忽略这一例外情况应该是安全的。 import java.io.*; import java.util.Arrays; import javax.swing.*; public class StandardOutReader extends JPanel{ JTextArea textArea = new JTextArea(); public StandardOutReader(){ add(textA

s、 您将得到该异常,因为流的一端总是在另一端之前关闭。忽略这一例外情况应该是安全的。
import java.io.*;
import java.util.Arrays;
import javax.swing.*;

public class StandardOutReader extends JPanel{

    JTextArea textArea = new JTextArea();

    public StandardOutReader(){
        add(textArea);

        try {
            //create all your inputs/outputs
            PipedOutputStream out = new PipedOutputStream();
            PipedInputStream in = new PipedInputStream(out);
            System.setOut(new PrintStream(out));
            final InputStreamReader reader = new InputStreamReader(in);


            //A thread that will continuously read the reader, and post the output to the GUI
            new Thread(new Runnable(){
                @Override
                public void run() {
                    try {
                        char[] cBuffer = new char[256]; //Buffer for reading
                        int bytesRead; // number of byts read in the last "read()" call
                        while((bytesRead = reader.read(cBuffer)) != -1){
                            //Copy over only the interesting bytes
                            final char[] copy = Arrays.copyOf(cBuffer, bytesRead);
                            //Tell the GUI to update
                            SwingUtilities.invokeLater(new Runnable(){
                                @Override
                                public void run() {
                                    textArea.append(new String(copy));
                                }});
                        }
                    }  catch (IOException e) {
                        e.printStackTrace();
                    }  finally{
                        //Closing the reader
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }}).start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new StandardOutReader());
        frame.pack();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        //Thread that randomly prints stuff.  Not part of the GUI stuff at all.
        new Thread(new Runnable(){
            @Override
            public void run() {
                for(int i = 0; i < 10; i++){
                    try {
                        Thread.sleep((long)(4000 * Math.random()));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Adding another phrase");
                }
            }}).start();
    }
}
PrintStream oldOut = System.out;

try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream newOut = new PrintStream(baos);

    System.setOut(newOut);

    OtherLib.init();

    textArea.setText(baos.toString());
} finally {
    System.setOut(oldOut);
}