Java GUI问题

Yat*_*oel 0 java user-interface

我用GUI制作了一个java程序.现在我想在GUI上添加一个组件,我可以以与显示输出相同的方式显示我想要的任何内容

System.out.println();
Run Code Online (Sandbox Code Playgroud)

我可以在GUI上添加哪个组件以及如何在该组件上显示内容.

tan*_*ens 5

您可以定义打印到JTextArea的PrintStream:

    final JTextArea textArea = new JTextArea();
    PrintStream printStream = new PrintStream( new OutputStream() {
        @Override
        public void write( final int b ) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    textArea.append( "" + (char )b );
                    textArea.setCaretPosition( textArea.getText().length() );
                }
            });
        }
    } );
    System.setOut(printStream);
Run Code Online (Sandbox Code Playgroud)