简体   繁体   English

取消操作JOptionPane遇到麻烦

[英]Trouble with cancelling operation JOptionPane

I have a problem with my really long and messy code (If there is a better way then please tell me) but the real problem I have is I want my JOptionPane.showConfirmDialog() to cancel the rest of the operation after cancel is clicked, my class here is suppose to set the "newDate" object to null if the cancel option is clicked, so far that is not happening so I think some fresh eyes will help. 我的代码很长而且很乱(如果有更好的方法,请告诉我),但是我真正的问题是我希望我的JOptionPane.showConfirmDialog()在单击取消后取消其余的操作,我的班级是假设如果单击“取消”选项,则将“ newDate”对象设置为null,到目前为止还没有发生,因此我认为有些新鲜的想法会有所帮助。 Here is the code: 这是代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Object o = jComboBox1.getSelectedItem();
    String st = (String) o;
    Object o2 = jComboBox2.getSelectedItem();
    String st2 = (String) o2;
    newDate = new Dates(st, (int)jSpinner1.getValue(),
            (int)jSpinner2.getValue(), st2);
    switch (newDate.getDayOfWeek()) {
                    //Sundays
                    case "Sun":
                        if (newDate.getHour() == 11) {
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    //Mondays, Tuesdays, Wednesdays, Thursdays
                    case "Mon":
                    case "Tue":
                    case "Wed":
                    case "Thu":
                        if (newDate.getHour() == 9 || newDate.getHour() == 10 ||
                                newDate.getHour() == 11) {//9, 10, 11 am
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        } else if (newDate.getHour() == 3 || newDate.getHour() == 4 ||
                                newDate.getHour() == 5|| newDate.getHour() == 6) {//3, 4, 5, 6 pm
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("pm")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    //Fridays
                    case "Fri":
                        if (newDate.getHour() == 9 || newDate.getHour() == 10) {//9, 10
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        } else if (newDate.getHour() == 5|| newDate.getHour() == 6) {//5, 6 pm
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("pm")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    //Saturdays
                    case "Sat":
                        if (newDate.getHour() == 9 || newDate.getHour() == 10 ||
                                newDate.getHour() == 11) {//9, 10, 11 am
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }   
                        break;
                    default:
                        break;
                }
    dispose();
}     

Change the if statement to: 将if语句更改为:

   if (m == JOptionPane.CLOSED_OPTION || m == JOptionPane.CANCEL_OPTION) {
       newDate = null;
       break;
   }

The CANCEL_OPTION has an int value of 2 while the conditional as it stands checks for 1 . CANCEL_OPTION的int值为2而条件CANCEL_OPTION则检查1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM