取消操作JOptionPane时出现问题

时间:2014-02-15 23:09:22

标签: java return joptionpane

我有一个问题,我的代码真的很长而且很混乱(如果有更好的方法请告诉我)但我真正的问题是我希望我的JOptionPane.showConfirmDialog()在取消后取消其余的操作单击,我的类在这里假设如果单击取消选项,将“newDate”对象设置为null,到目前为止还没有发生,所以我认为一些新鲜的眼睛会有所帮助。这是代码:

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

1 个答案:

答案 0 :(得分:0)

将if语句更改为:

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

CANCEL_OPTION的int值为2,而条件值则为1