java - 当用户单击取消时,如何使用 JOptionPane 从子菜单返回主菜单?
问题描述
我正在制作一种方法,其中有多个选项,并且每个选项一旦被选中,都需要让“取消”按钮返回主菜单。
主菜单:
public static void start(){
String[] choices = {"1. Routines for Triangles","2. Routines for Temperatures","3. Routines for Geometric Figures","4. String Manipulations",
"5. Miscellaneous Simple Games.","6. Quit"};
String menu = (String) JOptionPane.showInputDialog(null,"choose","Main menu",JOptionPane.QUESTION_MESSAGE,null,choices,choices[0])
while (menu != null){
switch (menu){
case "1. Routines for Triangles":
triangleRoutines(); //one sub menu
break;
case "2. Routines for Temperatures":
break;
case "3. Routines for Geometric Figures":
break;
case "4. String Manipulations":
break;
case "5. Miscellaneous Simple Games.":
break;
case "6. Quit":
break;
}
}
}
子菜单:
private static void triangleRoutines(){
String[] stringArray = {"Determine the Type of Triangle", "Determine a Valid Triangle", "Determine the Area of the Sides of a Triangle", "Determine a Pythagorean Triple",
};
String reply = (String) JOptionPane.showInputDialog(null, "Choose what you want to do today", "Triangle Processes", JOptionPane.QUESTION_MESSAGE,
null, stringArray, stringArray[0]);
switch (reply){
case "Determine the Type of Triangle":
break;
case "Determine a Valid Triangle":
break;
case "Determine the Area of the Sides of a Triangle":
break;
case "Determine a Pythagorean Triple":
break;
}
}
解决方案
当JOptionPane.showInputDialog()对话框在未进行选择的情况下被取消或关闭时,将返回null。你需要处理这个 null 并相应地采取行动,但你需要决定当它发生时你想做什么。关闭应用程序,重新显示主菜单对话框并强制选择退出菜单选项等。
要始终保持主菜单可用,请将其放在while循环中,例如:
String[] choices = {"1. Routines for Triangles", "2. Routines for Temperatures",
"3. Routines for Geometric Figures", "4. String Manipulations",
"5. Miscellaneous Simple Games.", "6. Quit"};
String menu = "";
while (!menu.equals("6. Quit")) {
menu = (String) JOptionPane.showInputDialog(null, "<html>Choose A Menu Item:<br><br></html>", "Main Menu",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
if (menu == null) {
//continue; //uncomment and delete line below to force menu item 6 to be selected to quit Main Menu.
break; //Quit the Main Menu
}
switch (menu) {
case "1. Routines for Triangles":
triangleRoutines(); //one sub menu
break;
case "2. Routines for Temperatures":
break;
case "3. Routines for Geometric Figures":
break;
case "4. String Manipulations":
break;
case "5. Miscellaneous Simple Games.":
break;
case "6. Quit":
// This case is not really required unless you want to
// do some cleanup of sorts before quiting.
break;
}
}
对包含子菜单的方法使用相同的技术,但添加一个菜单选项:如果需要Return To Main Menu
,可能还有一个附加选项:Quit Application
推荐阅读
- sql - 如何从 R 中的 SQL 导出数据
- javascript - 打字。如何让内容不下移?
- hive - 如何在 Cloudera 中建立 Grafana 和 Hive 连接
- react-native - 无法读取未定义的属性“getTrackDrawable”
- javascript - 节点:getaddrinfo ENOTFOUND (localhost:5000) 在不同的 localhost:8888 上运行
- optaplanner - 使用 optaplanner 解决 VRPMT 问题的最佳实践是什么?
- installation - 来自 nrf528040 的北欧半导体广告
- angular - 为什么 openapi-generator 不使用 typescript-angular 创建 package.json?
- reactjs - React 渐进式 Web 应用程序中的 Google 登录问题
- validation - Laravel 验证:仅在字段存在时验证