在执行.bat文件时启用按钮 [英] Enabling buttons while executing .bat file
本文介绍了在执行.bat文件时启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有打开和后退按钮的表格.我通过打开"按钮打开批处理文件,而批处理文件正在执行时,其他按钮均被禁用.我要启用这些按钮.请帮助我.
I have a form with an open and a back button. I open my batch file through the open button, while the batch file is executing, the other buttons are disabled. I want to enable these buttons. Please help me.
运行批处理文件代码:
private void openActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// back.setEnabled(true);
String filename = "C:\\JMeter Project\\jakarta-jmeter-2.5.1\\bin\\jmeter.bat";
String command = filename;
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
System.out.println(line);
}
back.setEnabled(true);
JOptionPane.showMessageDialog(null, "Batch file executed successfully.....!!!!");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Batch file execution failed.");
}
// Form f=new Form();
// back.action(f.setVisible(true),null);
}
后退按钮代码:
private void backActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
close();
sms_sound s = new sms_sound();
//s.setVisible(true);
Form f = new Form();
f.setVisible(true);
// back.setEnabled(true);
}
推荐答案
您正在阻止事件分发线程,这阻止了该线程处理任何重画请求.
You're blocking the event dispatching thread, which is preventing it from processing any repaint requests.
You should move you "batch" code to a background thread, something like a SwingWorker
would be excellent for this problem.
添加了示例
public class BatchRunner extends SwingWorker<Integer, String> {
@Override
protected Integer doInBackground() throws Exception {
String filename = "C:\\JMeter Project\\jakarta-jmeter-2.5.1\\bin\\jmeter.bat";
String command = filename;
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
publish(line);
}
return process.exitValue();
}
@Override
protected void process(List<String> chunks) {
// You can process the output produced
// the doBackgroundMethod here within the context of the EDT
}
@Override
protected void done() {
try {
Integer result = get();
if (result == 0) {
JOptionPane.showMessageDialog(null, "Batch file executed successfully.....!!!!");
} else {
JOptionPane.showMessageDialog(null, "Batch file returned an exit value of " + result);
}
} catch (InterruptedException | ExecutionException | HeadlessException interruptedException) {
JOptionPane.showMessageDialog(null, "Batch file execution failed.");
}
back.setEnabled(true);
}
}
当您准备执行批处理程序时,只需执行以下操作……
When you're ready to execute your batch program, simply do something like...
back.setEnabled(false);
new BatchRunner().execute();
这篇关于在执行.bat文件时启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文