【发布时间】:2020-03-09 16:09:04
【问题描述】:
我有一个项目,它在控制台中逐行打印出数字,我成功地将它重定向到我用于此应用程序的 GUI Jframe。但是,当数字打印到 TextArea 中时,它们并不会像滚动列表那样一一显示。相反,我看到整个 TextArea 一遍又一遍地闪烁和打印。阅读完成后,TextArea 中的一切看起来都是正确的。有没有办法正确设置它,让它打印得像我在控制台中看到的一样?
非常感谢您提供的任何帮助!
对于 system.out 的重定向,我有以下代码:
package ibanchecker03;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
public class CustomOutputStream extends OutputStream {
private JTextArea textArea;
public CustomOutputStream(JTextArea textArea) {
this.textArea=textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.append(String.valueOf((char)b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getDocument().getLength());
// keeps the textArea up to date
textArea.update(textArea.getGraphics());
}
}
然后在应用程序类中我有这个来重定向输出:
PrintStream printStream = new PrintStream(new CustomOutputStream(display));
System.setOut(printStream);
System.setErr(printStream);
【问题讨论】:
-
您帖子的正文和标题大约是
TextField。代码显示JTextArea。请edit您的帖子修复它并制作您的代码minimal reproducible example -
感谢您通知我,已更正。 .)
标签: java swing console system.out printstream