gpt4 book ai didi

java - 将批处理文件输出到 JTextArea

转载 作者:行者123 更新时间:2023-12-01 11:27:57 24 4
gpt4 key购买 nike

我设置了一些代码,据说可以运行批处理文件。我不确定,因为它没有在控制台中显示任何内容,但是当我单击 JButton PING 时,该按钮会保持单击状态几秒钟,因此它肯定正在处理某些内容。我需要帮助的是将批处理文件输出到 GUI 中的 JTextArea。我不太确定如何将我的代码定向到名为“textarea”的 JTextArea。有人可以告诉我如何将 textarea 添加到此代码中以获得输出吗?谢谢!

JButton btnPingComputer = new JButton("PING");
btnPingComputer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
// create a new process
// System.out.println("Creating Process...");
Process p = Runtime.getRuntime().exec("c:\\ping.bat");

// get the input stream of the process and print it
InputStream in = p.getInputStream();
for (int i = 0; i < in.available(); i++) {
System.out.println("" + in.read());
}
for (int i = 0; i < in.available(); i++) {
textArea.append(in.read()+"\n");
}
// wait for 10 seconds and then destroy the process

p.destroy();

} catch (Exception ex) {
ex.printStackTrace();
}




}
});

最佳答案

试试这个:

for (int i = 0; i < in.available(); i++) {
textarea.append(in.read()+"\n");
}

编辑:我认为 in.available() 也可能存在问题。您可以尝试完全更改此设置:

String line;
Process p = Runtime.getRuntime().exec("c:\\ping.bat");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
textarea.append(line);
}
in.close();

关于java - 将批处理文件输出到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30655309/

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