有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用cancel按钮对JOptionPane InputDialog循环进行转义

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

 /////////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) 个答案

  1. # 1 楼答案

    你到底想打破哪个循环

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

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