0

destroyProcesses(processes);因此,当我单击 my stopButtonwhich is a时,我想做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) {
            }
        }
    }

有任何想法吗?

4

2 回答 2

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
    }        
}

希望有帮助!

于 2013-07-04T08:52:39.587 回答
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) {
            }
        }
    }
}
于 2013-07-04T09:14:18.493 回答