2013-03-27 57 views
0

我有以下測試另一個類(MyDate)的類(testDate),當這3行被執行時,inputDialog框沒有關閉取消或稍微'x'右上角。Java - 當按下取消或'x'時,我的JOptionPane沒有關閉

myDay = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the day of the month, you're travelling on: ")); 
myMonth = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the month (1-12), you're travelling on: ")); 
myYear = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the year, you're travelling on: ")); 

以下是完整的testDate類:

import javax.swing.JOptionPane; 

public class testDate { 
public static void main(String[] args){ 
    int myDay = 0, myMonth = 0, myYear = 0; // initialising variables 
    boolean dateCorrect = false; 
    MyDate userTravelDate; 

    do { 
     try { 
      do { 
       myDay = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the day of the month, you're travelling on: ")); 
       myMonth = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the month (1-12), you're travelling on: ")); 
       myYear = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the year, you're travelling on: ")); 
       userTravelDate = new MyDate(myDay, myMonth, myYear); 
      } while (userTravelDate.isDateValid(userTravelDate.formDate()) == false); 
      dateCorrect = true; 
     } 
     catch (Exception e) { 
      JOptionPane.showMessageDialog(null, "Please enter only integers!", "Error Occurred!", JOptionPane.ERROR_MESSAGE); 
     } 
    } while (dateCorrect == false); 
    JOptionPane.showMessageDialog(null, myDay + "-" + myMonth + "-" + myYear);  
} 

}

我想知道,如果輸入對話框可以被關閉,如果我按「X」或取消,因爲在當'x'或者取消被按下的時候它執行該行:

JOptionPane.showMessageDialog(null, "Please enter only integers!", "Error Occurred!", JOptionPane.ERROR_MESSAGE); 

並且持續循環詢問白天,月份和年份,直到這些都是正確的。

回答

2

你可以檢查的showInputDialogbreak響應圈外在沒有響應

String response = JOptionPane.showInputDialog(...); 
if (response == null) { 
    break; // X pressed! 
} else { 
    myDay = Integer.parseInt(result); 
    ... 
} 
+0

哦,我看到的,謝謝。 – Khalid 2013-03-27 15:55:28

+0

不客氣:) – Reimeus 2013-03-28 11:25:52