我有一个项目,它在控制台中逐行打印数字,并且我成功地将其重定向到我用于此应用程序的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);