Java fx打印文本

时间:2014-03-25 18:47:28

标签: java javafx system.out

java fx的新手。我的问题是,有没有办法让我通过System.out打印的所有文字进入文本区域?我应该将所有打印件放入一个方法中,还是必须将each one传递给textarea?

1 个答案:

答案 0 :(得分:3)

您可以更改输出流,请遵循以下代码:

    TextArea ta = new TextArea();
    Scene scene = new Scene(ta);

    System.setOut(new PrintStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            ta.appendText("" + ((char)b));
        }

        @Override
        public void write(byte[] b) throws IOException {
            ta.appendText(new String(b));
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            ta.appendText(new String(b, off, len));
        }
    }));

    stage.setScene(scene);
    stage.show();
    System.out.println("THIS WILL BE WRITEN INSIDE THE TEXT AREA!");