2012-08-13 59 views
0

如果用戶單擊取消,我將如何爲循環添加一個退出方式。如果任何人願意有一段註釋掉的代碼,它在代碼的遊客輸入按鈕部分被引用,但它不起作用。由於使用取消按鈕跳轉到JOptionPane InputDialog循環

/////////Convert 
    public static int[] convertIntegers(java.util.List<Integer> elemIntegers) 
{ 
    int[] elements = new int[elemIntegers.size()]; 
    for (int i=0; i < elements.length; i++) 
    { 
     elements[i] = elemIntegers.get(i).intValue(); 
    } 
    return elements; 
} 

完整的源代碼是在這裏:http://pastebin.com/r4tEeatu

的特定部分是

insertTableF.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      int wayPoint1 = 0; 
      int wayPoint2 = 0; 
      int PassTime = 0; 
      Statement statementR; 

      if (loggedIn == 1) { 

       while (passedR == 0) { 
        try { 
         if (wayPoint1 == 0) { 
          sTagR = JOptionPane.showInputDialog("Please enter the Rhino Tag number:"); 
          iTagR = Integer.parseInt(sTagR); 
          wayPoint1 = 1; 
         } 
         if (wayPoint2 == 0) { 
          sGPSX = JOptionPane.showInputDialog("Please enter the horizontal GPS Grid Numbers(eg.3123):"); 
          iGPS = Integer.parseInt(sGPSX); 

          wayPoint2 = 1; 
         } 

         sGPSY = JOptionPane.showInputDialog("Please enter the vertical GPS Grid Letters(eg.XXYY:"); 
         while (PassTime == 0) { 
          sTime = JOptionPane.showInputDialog("Please enter the Last date you saw the Rhino(YYYY-MM-DD):"); 
          //  if (!sTime.equals("") || sTime !=null){ 
          if (isValidDate(sTime)) { 
           PassTime = 1; 
          } else { 
           JOptionPane.showMessageDialog(null, "Please use the date format YYYY-MM-DD."); 
          } 
          //  } else { 
          //    JOptionPane.showMessageDialog(null, "Please use the date format YYYY-MM-DD."); 
          //  } 
         } 
         sLocation = JOptionPane.showInputDialog("Please enter the Last place you saw the Rhino:"); 

         passedR = 1; 
        } catch (NumberFormatException nfe) { 
         passedR = 0; 
         JOptionPane.showMessageDialog(null, "Please use numbers for the Rhino Tag field."); 
        } 
       } 
       if (passedR == 1) { 
        ComboGPS = iGPS + " " + sGPSY; 
        try { 
         Connection insertTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", ""); 
         String sql = "insert into TblRhinoLoc values ('" + iTagR + "', '" + ComboGPS + "', '" + sTime + "', '" + sLocation + "')"; 

         try { 
          statementR = insertTable.createStatement(); 
          statementR.executeUpdate(sql); 

          JOptionPane.showMessageDialog(null, "Data entered successfully!"); 
          wayPoint1 = 0; 
          wayPoint2 = 0; 
         } catch (Exception err) { 
          System.out.println(err); 
          JOptionPane.showMessageDialog(null, err); 
         } 
         UpdateJTableRanger(); 
        } catch (SQLException er) { 
         System.out.println("Error: " + er); 
        } 

       } else { 

       } 
      } else { 
       JOptionPane.showMessageDialog(null, "Please make sure you are logged in before editing sensitive data."); 
      } 
     } 
    }); 

回答

1

究竟你想擺脫這循環?

我假設你想退出while (passedR == 0)循環,當用戶取消JoptionPane.showInputDialog選項之一。

while (passedR == 0) { 
... 
      sTagR = JOptionPane.showInputDialog(...); 
      if (sTagR == null) { 
       // User canceled. 
       break; 
      } else { 
       ... 
      } 
} 
+0

好吧,我試試看,謝謝 – AceFire6 2012-08-13 19:31:44

+0

這個工作,非常感謝你 – AceFire6 2012-08-13 19:57:53