2017-03-10 13 views
0

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; 
      } 
     } 
    } 
} 

제안 사항을 알려주십시오.

답변

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 -method

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]); 
} 
관련 문제