java系统。用PipedOutputStream开始
我用JTextArea开发了一个小控制台。我读了一些教程,知道一些事情。但我还是有个问题。这是我的密码
public class Console extends JFrame {
public Console() throws IOException {
setSize(492, 325);
setLayout(new BorderLayout());
setDefaultCloseOperation(3);
setVisible(true);
final JTextArea area = new JTextArea();
add(area, BorderLayout.CENTER);
JButton button = new JButton("test");
add(button, BorderLayout.EAST);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Test with Button Click.."); // this is not print in textarea.
}
});
final PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
System.out.println("Test Before setOut.");
System.setOut(new PrintStream(pos, true));
System.out.println("Test After setOut."); // this is printed in my textarea.
new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
Scanner scan = new Scanner(pis);
while (scan.hasNextLine())
area.append(scan.nextLine());
return null;
}
}.execute();
}
public static void main(String[] args) throws Exception {
new Console();
}
}
这是我的输出
单击按钮后,系统将关闭。出来println不适用于textarea。我不知道我做错了什么
# 1 楼答案
我发现使用管道流令人困惑。你可以查看Message Console了解不同的方法