gpt4 book ai didi

java - 使用取消按钮转义 JOptionPane InputDialog 循环

转载 作者:行者123 更新时间:2023-12-01 15:13:43 26 4
gpt4 key购买 nike

如果用户单击“取消”,我将如何添加一种退出循环的方法。如果有人愿意,可以有一个注释掉的代码部分,并且在代码的“游客输入按钮”部分中引用它,但它不起作用。谢谢

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

最佳答案

您到底想跳出哪个循环?

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

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

关于java - 使用取消按钮转义 JOptionPane InputDialog 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11940560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com