我想用啓動和停止進程按鈕來創建GUI。在點擊開始按鈕時,一個進程啓動,當用戶點擊停止按鈕時,它應該停止正在運行的進程,但是當進程開始控制時,不會返回到原始GUI。 任何人都可以有解決方案嗎? 代碼片段如下: -java-如何停止點擊按鈕上的進程
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(EVT java.awt.event.ActionEvent中){
// TODO添加處理代碼在這裏:
process.destroy();
}