如何将代码带回主菜单?

时间:2017-03-10 23:13:04

标签: java menu joptionpane

我在Java中创建了一个程序,试图创建一个弹出菜单,为您提供选择。

我做出了选择以及一切如何运作但我似乎无法理解,当您完成用户名和密码的创建时,它会将您带回主菜单。

对此有何看法?

import javax.swing.*;

public class MyOwn {

    /**
     * @author Noah
     */
    public static void main(String[] args) {
        Object[] options = {
            "Create Login",
            "Login",
            "Quit"
        };
        int menuOption = JOptionPane.showOptionDialog(null, "Please choose the " + "program you would like to run:", "Program Menu", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
        boolean exit = false;
        while (!exit) {
            switch (menuOption) {
            case 0:

                String userName = JOptionPane.showInputDialog(null, "Create a username:", "johndoe");
                String password = JOptionPane.showInputDialog(null, "Create a password:", "johndoe123");
                exit = true;
                break;
            case 1:
                String userNameEntry = JOptionPane.showInputDialog(null, "Enter your username:");
                String passwordEntry = JOptionPane.showInputDialog(null, "Enter your password:");
                if (userName.equals(userNameEntry) && password.equals(passwordEntry)) {

                    Object[] optionsTwo = {
                        "Season Quiz",
                        "Profile Analysis",
                        "Piggy Bank"
                    };
                    int menuOptionTwo = JOptionPane.showOptionDialog(null, "Please choose the " + "program you would like to run:", "Program Menu", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, optionsTwo, optionsTwo[2]);
                    switch (menuOptionTwo) {
                    case 0:
                        System.out.println("Season Quiz");
                        break;
                    case 1:
                        System.out.println("Profile Analysis");
                        break;
                    case 2:
                        System.out.println("Piggy Banks");
                        break;
                    }
                }
                break;
            case 2:
                System.exit(0);
            default:
                break;
            }
        }
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我不知道我是否帮助你。我在创建用户名和密码后显示主菜单,然后在登录后再次显示。

Object[] options = {"Create Login", "Login", "Quit"};
JOptionPane optionPane = new JOptionPane();

int menuOption = showMenu(options);
boolean exit = false;
String userName = null;
String password = null;
while(!exit){
    switch (menuOption) {
        case 0:

            userName = JOptionPane.showInputDialog(null,"Create a username:","johndoe");
            password = JOptionPane.showInputDialog(null,"Create a password:","johndoe123");
            menuOption = showMenu(options);//Show MainMenu again
            break;
        case 1:
            String userNameEntry = JOptionPane.showInputDialog(null,"Enter your username:");
            String passwordEntry = JOptionPane.showInputDialog(null,"Enter your password:");
            if(userName == null || password == null){
                System.err.println("userName or password not set");
                exit = true;
                break;
            }
            if (userName.equals(userNameEntry) && password.equals(passwordEntry) ) {

                Object[] optionsTwo = {"Season Quiz", "Profile Analysis", "Piggy Bank"};
                int menuOptionTwo = showMenu(optionsTwo);//Show SpecialMenu
                switch (menuOptionTwo) {
                    case 0:
                        System.out.println("Season Quiz");
                        break;
                    case 1:
                        System.out.println("Profile Analysis");
                        break;
                    case 2:
                       System.out.println("Piggy Banks");
                        break;
                }
                menuOption = showMenu(options);//Show MainMenu
            }
            break;
        case 2:
            exit = true;
            System.exit(0);
        default:
            break;
    }
}

这里是showMenu - 方法

private static int showMenu(Object[] options) {
    return JOptionPane.showOptionDialog(null, "Please choose the "
                    + "program you would like to run:","Program Menu",JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,null,options,options[2]);
}