2016-07-17 7 views
0

JOptionPane에이 코드 단편이 있습니다. Yes 버튼을 클릭하면 다른 프레임을 열고 No 또는 Cancel을 클릭하면 프레임을 닫고 싶습니다.JOptionPane 버튼 없음의 프레임 닫기

케이스 1과 케이스 2를 System.exit (0)으로 설정하기 전에; 케이스 0은 다른 프레임을 성공적으로 열기 때문에 완벽하게 작동합니다. 그러나 예 버튼을 클릭했을 때 system.exit을 case 1과 2에 둘 때 프레임이 여전히 닫힙니다.

int test = JOptionPane.showConfirmDialog(null, "You lost! Play again?"); 

     switch(test) { 
      case 0: RPS rps = new RPS(); 
         rps.setVisible(true); 
         this.dispose(); //Yes option 
      case 1: System.exit(0); //No option 
      case 2: System.exit(0); //Cancel option 
      } 

내가 뭘 잘못 했니?

+1

'break' 문이 누락 – Reimeus

답변

1

코드에 break 문을 입력하는 것을 잊어 버렸습니다. 편집 후

, 코드는 다음과 같이 수 :

int test = JOptionPane.showConfirmDialog(null, "You lost! Play again?"); 
switch(test) { 
case 0: RPS rps = new RPS(); 
     rps.setVisible(true); 
     this.dispose(); // Yes option 
     break; 
case 1: System.exit(0); // No option 
case 2: System.exit(0); // Cancel option 
} 

다음과 같이의 JOptionPane에서 제공하는 상수를 사용하는 것이 더 좋다 :

int test = JOptionPane.showConfirmDialog(null, "You lost! Play again?"); 
switch(test) { 
case YES_OPTION: RPS rps = new RPS(); 
       rps.setVisible(true); 
       this.dispose(); // Yes option 
       break; 
case NO_OPTION: System.exit(0); // No option 
case CANCEL_OPTION: System.exit(0); // Cancel option 
}