0

我正在开发一个 Java 程序,我想在用户单击按钮时执行一些命令行代码。我必须编写代码来执行我的命令行代码和按钮代码,但我现在不知道如何将两者结合起来:我的按钮代码如下所示:

    private JButton setup;
public ProgramGUI(){
    UsedHandler handler = new UsedHandler();
    setup.addActionListener(handler);

}
private class UsedHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent event) {
        if(event.getSource()==setup)
            JOptionPane.showMessageDialog(null, "Everything fine!");
    }
}

这就是我的命令行代码:

            try {
            Runtime rt = Runtime.getRuntime();
            //Process pr = rt.exec("cmd /c dir");
            Process pr = rt.exec("c:\\helloworld.exe");

            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

            String line=null;

            while((line=input.readLine()) != null) {
                System.out.println(line);
            }

            int exitVal = pr.waitFor();
            System.out.println("Exited with error code "+exitVal);

        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }

谢谢!

4

2 回答 2

2

为防止命令行代码占用大量时间而阻塞AWT,需要多线程。

private class UsedHandler implements ActionListener, Runnable {
    private JButton setup;
    private Executor executor = Executors.newSingleThreadExecutor();

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == setup) {
            executor.execute(this);
        }
    }

    @Override
    public void run() {
        try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("c:\\helloworld.exe");
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(pr.getInputStream()));
            String line = null;
            while ((line = input.readLine()) != null)
                System.out.println(line);
            int exitVal = pr.waitFor();
            System.out.println("Exited with error code " + exitVal);
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}
于 2013-04-23T14:06:19.997 回答
0

将您的 exec 代码放入一个方法中,并从您的按钮执行的操作中调用它

public void yourProcess() {
try {
        Runtime rt = Runtime.getRuntime();
        //Process pr = rt.exec("cmd /c dir");
        Process pr = rt.exec("c:\\helloworld.exe");

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);

    } catch(Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}

然后使用方法

 @Override
public void actionPerformed(ActionEvent event) {
    if(event.getSource()==setup){
        yourProcess();
        JOptionPane.showMessageDialog(null, "Everything fine!");
    }
}
于 2013-04-23T14:02:10.710 回答