2011-10-08 6 views
2

신청이 있습니다. 이 버튼을 클릭해야 사용자가 닫기 버튼을 클릭 할 때 확인 메시지가 표시됩니다 (자바 스크립트 확인 대화 상자와 동일), '정말입니까?' Netbeans에서 Swing 비주얼 편집기를 사용하고 있으며 윈도우 닫기에 리스너를 사용하고 있습니다. 그러나 나는 그것을 만들 수 없습니다. 어떤 도움이 필요합니까?자바 만들기 확인 출구

+2

이 질문과 gazillion 시간을 응답 한 것처럼 첫 번째 포럼을 검색하십시오. 예를 들어, [How-can-a-swing-windowlistener-veto-jframe-closing] (http://stackoverflow.com/questions/3777146/how-can-a-swing-windowlistener-veto-jframe-closing). 또한 WindowListner (올바른 접근법)를 사용하고 있다고 말하면서 "그러나 그것을 만들 수는 없습니다"라고 말한 다음, 시도한 것을 보여주지 마십시오. 그렇게하지 않으면, 우리가 잘못하고있는 것을 말할 수 없습니다. –

+0

죄송합니다, "WindowListener" –

+0

@HovercraftFullOfEels 그 당시 내 검색어에 대한 올바른 키워드를 찾을 수 없습니다. 제안 주셔서 감사합니다 ... – Dewsworld

답변

6

이 코드를보십시오 :

jj4.addActionListener(new ActionListener() // jj4-button's reference to implement exit 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      if(!jtextarea.getText().equals("") && !jtextarea.getText().equals(fileContent)) 
      { 
       if(fileName == null) 
       { 
        //this method have 3 options (1 = YES, 2 = NO, 3 = Cancel) 
        option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??"); 
        if(option == 0) 
        { 
         //to save the text into file 
         saveAs(); 
         System.exit(0); 
        } 
        if(option == 1) 
        { 
         //to exit the program without saving 
         System.exit(0); 
        } 
       } 
       else 
       { 
        option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??"); 
        if(option == 0) 
        { 
         save(); 
         System.exit(0); 
        } 
        if(option == 1) 
        { 
         System.exit(0); 
        } 
       } 
      } 
      else 
      { 
       System.exit(0); 
      } 
     } 
    }); 
+3

마술 숫자를 사용하지 마십시오! JOptionPane는, Yes, No 및 Cancel를 체크하기위한 변수를 정의했습니다. – camickr