我有一个将信息显示为JSON(astext)的程序。在netbeans consol中,输出看起来像这样:
------------------------
| res1 |
========================
| "Houben,-Henriette" |
| "Maiterth,-Ralf" |
| "Müller,-Heiko" |
| "Herr,-Hansjörg" |
| "Schneider,-Georg" |
------------------------
但是JTextAreaInterface中的结果如下所示:
----------------
| res1 |
==================
| "Houben,-Henriette" |
| "Maiterth,-Ralf" |
| "Müller,-Heiko" |
| "Herr,-Hansjörg" |
| "Schneider,-Georg |
------------------------
当我得到两或三列结果时,问题就更糟了。
有办法解决吗?
这是我的JTextAreaInterface:
public class JTextAreaInterface extends OutputStream {
public static JTextArea textArea = new JTextArea(25, 80);
private final JTextArea destination;
public JTextAreaInterface(JTextArea destination) {
if (destination == null)
throw new IllegalArgumentException("Destination is null");
this.destination = destination;
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException {
final String text = new String(buffer, offset, length);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
destination.append(text);
}
});
}
@Override
public void write(int b) throws IOException {
write(new byte[] { (byte) b }, 0, 1);
}
public static void main(String[] args) throws Exception {
textArea.setEditable(false);
JFrame frame = new JFrame("Processing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
JTextAreaInterface out = new JTextAreaInterface(textArea);
System.setOut(new PrintStream(out));
MyProgram test = new MyProgram();
test.Processing();
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
}
最佳答案
您需要使用JTextArea.setFont()为文本区域设置等宽字体,例如Courier。