【发布时间】:2011-08-19 06:13:10
【问题描述】:
我正在尝试使用开始和停止进程按钮创建 GUI。单击开始按钮时,进程启动,当用户单击停止按钮时,它应该停止正在运行的进程,但是当进程启动时,控制永远不会返回到原始 GUI。 任何人都可以解决它吗? 代码片段如下:-
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
jTextArea1.setText("\nC:\\peach\\peach.bat --debug "+jFormattedTextField4.getText()+"\n\n");
if(jFormattedTextField4.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Browse The Peach File First");
}
else
{
String line=new String(jFormattedTextField4.getText());
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null;
// launch EXE and grab stdin/stdout and stderr
//process = Runtime.getRuntime ().exec("C:\\peach\\peach.bat --debug "+line);
stdin = process.getOutputStream ();
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
stdin.close();
// clean up if any output in stdout
BufferedReader brCleanUp = new BufferedReader (new InputStreamReader (stdout));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stdout] " + line);
jTextArea1.append("[Stdout]-->"+line+"\n");
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp = new BufferedReader (new InputStreamReader (stderr));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stderr]-->" + line);
jTextArea1.append("[Stderr]"+line+"\n");
}
brCleanUp.close();
}
}
catch (Exception err) {
err.printStackTrace();
}
}
私人无效 jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 在此处添加您的处理代码:
process.destroy();
}
【问题讨论】:
标签: java