如果用户点击取消,我将如何为循环添加退出方式?如果有人愿意,有一个注释掉的代码部分,它在代码的旅游输入按钮部分引用,但它不起作用。感谢
/////////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.");
}
}
});
答案 0 :(得分:1)
你想要突破哪个循环?
我假设您要在用户取消其中一个while (passedR == 0)
选项时退出JoptionPane.showInputDialog
循环。
while (passedR == 0) {
...
sTagR = JOptionPane.showInputDialog(...);
if (sTagR == null) {
// User canceled.
break;
} else {
...
}
}