当用户单击“取消”时,如何使用JOptionPane从子菜单返回主菜单?

时间:2018-10-20 01:12:46

标签: java swing 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;
           }
 }

2 个答案:

答案 0 :(得分:1)

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添加一个附加选项。

答案 1 :(得分:0)

最好的方法是实施Command design pattern

但是,如果要点是triangleRoutines需要返回到上一个菜单,那么考虑到当前的总体方法,我认为对显示菜单的位置进行修改是可行的。我对OP的问题并不肯定,但我相信。

有关更多详细信息,请参见示例代码中的注释。

未经测试,但类似:

private static void triangleRoutines() {
  ...

  // if the program is done, can return null

  // if cancel, then return anything but null
  return "";
}

然后

do {
  // get the menu from the top level
  String menu = (String) JOptionPane.showInputDialog(null,"choose","Main menu",JOptionPane.QUESTION_MESSAGE,null,choices,choices[0])
   switch (menu){
       case "1. Routines for Triangles":
           // if cancel, then will return empty string, and then
           //  re-present the main menu 
           triangleRoutines(); //one sub menu
           break;

       ...

       case "6. Quit":
           menu = null;
           break;
   } //switch
} while (menu != null);

因此,在triangleRoutines()中,可以“取消”,它将返回一个空的String,它不为null,因此它将再次显示顶级菜单。如果要退出子菜单triangleRoutines(),则返回null,程序将退出。

再次,我可能误解了具体问题。