我错过了一些东西,我创建了一个基于可调用的类,并在我的 gui 中创建了一个实例并在执行操作时调用它。唯一的问题是 GUI 没有响应,因为进程在同一个线程上运行,并且必须等待它完成才能访问 GUI。以下是部分代码:
public class MYProject {
public static class CreateProject implements Callable<Boolean>{
private String m_url;
private String m_project;
private String m_options;
public CreateProject(String url, String project, String options){
m_url = url;
m_project = project;
m_options = options;
}
public Boolean call(){
Boolean result = true;
try {
if (os.toLowerCase().contains("windows")){
command = windowsCMD + command;
}
String line;
Process p = Runtime.getRuntime().exec(command);
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader bri = new BufferedReader(isr);
try {
while ((line = bri.readLine()) != null) {
if(line.startsWith("ERROR")){
System.out.println(line);
result = false;
break;
}
}
p.waitFor();
}
finally {
bri.close();
}
}
catch (Exception err) {
System.err.println("Unable to create project: " + err.getMessage()
+ "\n");
result = false;
}
return result;
}
}
}
并在 GUI 中:
private void jButtonRefreshActionPerformed(java.awt.event.ActionEvent evt) {
jTextAreaConsole.append("Creating project.\n");
MYProject.CreateProject blah = new MYProject.CreateProject("url", "project", "options");
String result = blah.call();
jTextAreaConsole.append("Project creation successful: " + result);
}
结果是 Process p 仍然在与 gui 相同的线程上运行,并且没有任何内容是可点击的,并且 jTextAreaConsole 直到进程完成后才会更新。任何人都可以就我应该如何实施这一点提供一些建议吗?