0

如果用户单击取消,我将如何为循环添加退出方式。如果有人愿意,有一段注释掉的代码,它在代码的游客输入按钮部分中被引用,但它不起作用。谢谢

 /////////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.");
            }
        }
    });
4

1 回答 1

1

您究竟想跳出哪个循环?

我假设您想while (passedR == 0)在用户取消其中一个JoptionPane.showInputDialog选项时退出循环。

while (passedR == 0) {
...
            sTagR = JOptionPane.showInputDialog(...);
            if (sTagR == null) {
                 // User canceled.
                 break;
            } else {
               ...
            }
}
于 2012-08-13T19:11:30.260 回答