按下退出按钮时的事件:JOptionPane.showInputDialog
event when press exit button: JOptionPane.showInputDialog
点击取消或退出按钮后如何添加功能?我这样试过,但对我不起作用。
它向我显示一个错误,说 choice 是 null 但它不可能是因为它是 int?还有其他解决办法吗?
public void start() {
int choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Choice: \n 1 - Play a game \n 2 - show all games \n 3 - Find the best score \n 4 - Find a player \n 5 - End"));
//if (choice == JOptionPane.OK_OPTION) {
switch (choice) {
case 1:
this.play();
break;
case 2:
this.allGames();
break;
case 3:
this.getBestScore();
break;
case 4:
this.getPlayer();
break;
case 5:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Wrong input.");
break;
}
// } else if (choice == JOptionPane.CANCEL_OPTION) {
// System.exit(0);
// } else if (choice == JOptionPane.CLOSED_OPTION) {
// System.exit(0);
// }
}
当您按下“退出”或“取消”按钮时,您正在将“选择”分配给 null,但这不起作用,因为选择是一个整数。您可以将“选择”设为一个字符串,然后在 switch 语句之前确保选择!=null。这就是它的样子
public static void start() {
String choice = JOptionPane.showInputDialog(null, "Choice: \n 1 - Play a game \n 2 - show all games \n 3 - Find the best score \n 4 - Find a player \n 5 - End");
if(choice!=null) {
switch (choice) {
case "1":
play();
break;
case "2":
allGames();
break;
case "3":
getBestScore();
break;
case "4":
getPlayer();
break;
case "5":
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Wrong input.");
break;
}
}
}
点击取消或退出按钮后如何添加功能?我这样试过,但对我不起作用。
它向我显示一个错误,说 choice 是 null 但它不可能是因为它是 int?还有其他解决办法吗?
public void start() {
int choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Choice: \n 1 - Play a game \n 2 - show all games \n 3 - Find the best score \n 4 - Find a player \n 5 - End"));
//if (choice == JOptionPane.OK_OPTION) {
switch (choice) {
case 1:
this.play();
break;
case 2:
this.allGames();
break;
case 3:
this.getBestScore();
break;
case 4:
this.getPlayer();
break;
case 5:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Wrong input.");
break;
}
// } else if (choice == JOptionPane.CANCEL_OPTION) {
// System.exit(0);
// } else if (choice == JOptionPane.CLOSED_OPTION) {
// System.exit(0);
// }
}
当您按下“退出”或“取消”按钮时,您正在将“选择”分配给 null,但这不起作用,因为选择是一个整数。您可以将“选择”设为一个字符串,然后在 switch 语句之前确保选择!=null。这就是它的样子
public static void start() {
String choice = JOptionPane.showInputDialog(null, "Choice: \n 1 - Play a game \n 2 - show all games \n 3 - Find the best score \n 4 - Find a player \n 5 - End");
if(choice!=null) {
switch (choice) {
case "1":
play();
break;
case "2":
allGames();
break;
case "3":
getBestScore();
break;
case "4":
getPlayer();
break;
case "5":
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Wrong input.");
break;
}
}
}