0

私は別のクラス (MyDate) をテストする次のクラス (testDate) を持っています。

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

これらが正しくなるまで、日、月、年を尋ねるループを続けます。

4

1 に答える 1

2

応答がない場合は、ループの内外で応答showInputDialogを確認できますbreak

String response = JOptionPane.showInputDialog(...);
if (response == null) {
   break; // X pressed!
} else {
   myDay = Integer.parseInt(result);
   ...
}
于 2013-03-27T15:49:50.490 に答える