JPanel
を拡張し、JTextPane
を含むクラスがあります(下記参照)。 System.out
とSystem.err
をJTextPane
にリダイレクトします。私のクラスは動作していないようです。私はそれを実行すると、システムのプリントをリダイレクトしますが、私のJTextPane
にはプリントしません。助けてください!System.outをJTextPaneにリダイレクト
注:コールは、アプリケーションの起動時にのみリダイレクトされます。しかし、起動後いつでも、System.out
コールはJTextPane
にリダイレクトされません。 (すなわち、私がクラスにSystem.out.prinln();
を置くと呼び出されますが、後で使用するためにactionListener
に置かれていると、それはリダイレクトされません)。
public class OSXConsole extends JPanel {
public static final long serialVersionUID = 21362469L;
private JTextPane textPane;
private PipedOutputStream pipeOut;
private PipedInputStream pipeIn;
public OSXConsole() {
super(new BorderLayout());
textPane = new JTextPane();
this.add(textPane, BorderLayout.CENTER);
redirectSystemStreams();
textPane.setBackground(Color.GRAY);
textPane.setBorder(new EmptyBorder(5, 5, 5, 5));
}
private void updateTextPane(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Document doc = textPane.getDocument();
try {
doc.insertString(doc.getLength(), text, null);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
textPane.setCaretPosition(doc.getLength() - 1);
}
});
}
private void redirectSystemStreams() {
OutputStream out = new OutputStream() {
@Override
public void write(final int b) throws IOException {
updateTextPane(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextPane(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
}
私は間違っていたので私の答えを削除しました。 – jjnguy
一部の通話がリダイレクトされるのを見ていますか? – jjnguy
OSXConsoleクラスからの呼び出しのみがJTextPaneに出力されます。 – Jakir00