2012-04-17 3 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에 무엇이 있습니까? –

+0

이 숙제가 있습니까? – Aaron

+0

@pst 문제는 콘솔 창이 실행되지 않는다는 것입니다. 기술적으로 숙제가 완료되었습니다. –

답변

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

먼저 당신이 현재 실행중인 자바 응용 프로그램에 핸들이 필요하고 그래서 당신은 런타임 객체를 생성하고 Runtime.getRuntime를 사용 할(). 그런 다음 새 프로세스를 선언하고 exec 호출을 사용하여 적절한 응용 프로그램을 실행할 수 있습니다.

bufferReader는 생성 된 프로세스의 출력을 인쇄하여 Java 콘솔에서 인쇄하는 데 유용합니다.

마지막으로, pr.waitFor()는 진행하기 전에 프로세스 pr이 종료되기를 현재 스레드가 기다리게합니다. exitVal에는 오류 코드 (있는 경우)가 포함됩니다 (0은 오류 없음을 의미).

희망이 도움이됩니다.

+0

매우 유익한 광범위한 코드를 보내 주셔서 감사합니다. 그러나 콘솔 응용 프로그램은 실제로 제어판과 동일한 BlueJ 패키지의 다른 클래스입니다. –

+1

그런 다음 해당 클래스의 인스턴스를 만들고 적절한 함수를 호출하면 안되는 이유는 무엇입니까? – Chris911

+0

위의 코드가 그랬다고 생각했습니다. 내가 뭐 놓친 거 없니? –

관련 문제