【发布时间】:2013-11-27 15:23:13
【问题描述】:
我已经使用 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();
}
}
这是我的输出..
单击按钮时,System.out.println 无法与 textarea 一起使用。我不知道我做错了什么。
【问题讨论】:
标签: java swing console swingworker