所以当我点击destroyProcesses(processes);
stopButton
时,我想JButton
。我如何在我的代码中使用它?
这是我的代码:
private void Processes() throws IOException, InterruptedException {
// New Thread "processesThread" will start here.
final Object mon = threadBlock;
Thread processesThread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (mon) {
try {
try {
Runtime rt = Runtime.getRuntime();
List<Process> processes = new ArrayList<Process>();
// "runnableTogether" will be the number that the user inputs in the GUI.
switch (runnableTogether) {
case 4:
processes.add(rt.exec("C:/Windows/System32/SoundRecorder.exe"));
case 3:
processes.add(rt.exec("C:/Windows/System32/taskmgr.exe"));
case 2:
processes.add(rt.exec("C:/Windows/System32/notepad.exe"));
case 1:
processes.add(rt.exec("C:/Windows/System32/calc.exe"));
Thread.sleep(5000);
destroyProcesses(processes);
break;
default:
System.exit(0);
break;
}
mon.wait();
} catch (IOException ex) {
}
} catch (InterruptedException ex) {
}
}
}
});
processesThread.start();
// New Thread "processesThread" will end here.
}
private void destroyProcesses(List<Process> processes) {
if (processes == null) {
return;
}
else {
for (Process thisProcess : processes) {
thisProcess.destroy();
}
processes.clear();
}
}
public void actionPerformed(final ActionEvent e) {
if (e.getSource() == stopButton) {
try {
// Destroy processes here.
System.exit(0);
}
catch (Exception ex) {
}
}
}
有什么想法吗?
答案 0 :(得分:1)
您需要将流程作为实例变量,如下所示:
public class MyClass {
private List<Process> processes = new ArrayList<Process>();
public MyClass() {
initProcesses();
}
private void initProcesses() {
// init the processes here
}
public void actionPerformed(final ActionEvent e) {
// now here you can use the processes
}
}
希望有所帮助!
答案 1 :(得分:0)
将局部变量List<Process> processes = new ArrayList<Process>();
移动到类级别并在声明变量之前添加修饰符,在这种情况下,它是private
。
public class yourProgram {
private List<Process> processes = new ArrayList<Process>(); // This was moved from directly after setting up the Runtime.
private void Processes() throws IOException, InterruptedException {
// New Thread "processesThread" will start here.
final Object mon = threadBlock;
Thread processesThread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (mon) {
try {
try {
Runtime rt = Runtime.getRuntime();
// "runnableTogether" will be the number that the user inputs in the GUI.
switch (runnableTogether) {
case 4:
processes.add(rt.exec("C:/Windows/System32/SoundRecorder.exe"));
case 3:
processes.add(rt.exec("C:/Windows/System32/taskmgr.exe"));
case 2:
processes.add(rt.exec("C:/Windows/System32/notepad.exe"));
case 1:
processes.add(rt.exec("C:/Windows/System32/calc.exe"));
Thread.sleep(5000);
destroyProcesses(processes);
break;
default:
System.exit(0);
break;
}
mon.wait();
} catch (IOException ex) {
}
} catch (InterruptedException ex) {
}
}
}
});
processesThread.start();
// New Thread "processesThread" will end here.
}
private void destroyProcesses(List<Process> processes) {
if (processes == null) {
return;
}
else {
for (Process thisProcess : processes) {
thisProcess.destroy();
}
processes.clear();
}
}
public void actionPerformed(final ActionEvent e) {
if (e.getSource() == stopButton) {
try {
// Destroy processes here.
System.exit(0);
}
catch (Exception ex) {
}
}
}
}