2012-04-17 42 views
1

这是在BlueJ中创建的作业,并以包含BlueJ软件包的zip文件形式提交。如何从gui启动控制台程序?

在包中有几个独立的控制台程序。我正在尝试创建另一个“控制面板”程序 - 一个带单选按钮的gui来启动每个程序。

以下是我已经尝试了监听器类的2:提前

private class RadioButtonListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource() == arraySearchButton) 
     { 
      new ArraySearch(); 
     }//end if 
      else if(e.getSource() == workerDemoButton) 
      { 
       new WorkerDemo(); 
      }//end else if 
    }//end actionPerformed 
}//end class RadioButtonListener 

private class RunButtonListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     if(arraySearchButton.isSelected()) 
     { 
      new ArraySearch(); 
     }//end if 
      else if(workerDemoButton.isSelected()) 
      { 
       new WorkerDemo(); 
      }//end else if 
    }//end actionPerformed 
}//end class RunButtonListener 

谢谢!

+1

那么问题是什么?例如。发生了什么(或没有发生)?你怎么知道它应该被调用? WorkerDemo中有什么? – 2012-04-17 21:05:38

+0

这是功课吗? – Aaron 2012-04-17 21:08:07

+0

@pst 问题是控制台窗口从不启动。从技术上讲,作业是完整的,这是额外的。 – 2012-04-17 21:14:01

回答

1

假设您尝试启动.EXE控制台应用程序,以下是一些可以帮助您的代码。请参阅下面的解释。

import java.io.*; 


public class Main { 

     public static void main(String args[]) { 

      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(); 
      } 
     } 
} 

首先,你需要对当前正在运行的Java应用程序的处理和做,你创建一个Runtime对象,并使用调用Runtime.getRuntime()。然后您可以声明一个新进程并使用exec调用来执行正确的应用程序。

bufferReader将帮助打印生成的进程的输出并将其打印在java控制台中。

最后,pr.waitFor()会强制当前线程在继续前等待进程pr终止。如果有的话exitVal包含错误代码(0表示没有错误)。

希望这会有所帮助。

+0

感谢您的广泛代码,非常翔实。然而,控制台应用程序实际上是与控制面板相同的BlueJ软件包中的其他类。 – 2012-04-17 21:17:18

+1

然后,为什么不能简单地创建该类的实例并调用正确的函数? – Chris911 2012-04-17 21:21:41

+0

我认为这就是上面的代码所做的;我错过了什么? – 2012-04-17 22:18:03

相关问题