2014-02-15 33 views
0

我有一個問題,我真的很漫長的代碼(如果有更好的方法,那麼請告訴我)但我真正的問題是我想我的JOptionPane.showConfirmDialog()取消其餘點擊取消後的操作,我的類在這裏假設設置「newDate」對象爲null,如果單擊取消選項,迄今爲止沒有發生,所以我認爲一些新鮮的眼睛會有所幫助。下面是代碼:取消操作的麻煩JOptionPane

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

回答

0

更改if語句:

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

CANCEL_OPTION2 int值而有條件的,因爲它代表檢查1