gpt4 book ai didi

java - 尝试在 JTextAreaInterface 中显示 JSON (asText) 输出

转载 作者:行者123 更新时间:2023-11-30 03:00:25 24 4
gpt4 key购买 nike

我有一个将信息显示为 JSON (astext) 的程序。在 netbeans 控制台中,输出看起来不错,如下所示:

------------------------
| 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。

关于java - 尝试在 JTextAreaInterface 中显示 JSON (asText) 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36148472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com