java - Joptionpane 不处理 switch-case

标签 java swing switch-statement joptionpane

我的 switch-case 选项和 joptionpane 遇到问题。选择四个选项之一后,程序结束。它不显示选择。它应该显示选择/选择。

  import javax.swing.JOptionPane; // JOptionPane call

  public class OnlineStore 
  {
     // public static void main(String[] args) // main program
      public void display_menu() // Not the main program but the main menu.
      {
          String main_selection;
          int mSelect;

          main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
    mSelect = Integer.parseInt(main_selection);

      }

      public OnlineStore() // Switch-case program
      {
          display_menu(); // Call display menu.
          switch (mSelect)
          {
              case 1:
                  JOptionPane.showMessageDialog(null, "Option 1");
                  break;
              case 2:
                  JOptionPane.showMessageDialog(null, "Option 2");
                  break;
              case 3:
                  JOptionPane.showMessageDialog(null, "Option 3");
                  break;
              case 4:  // Deliberately not including a default selection.
                  JOptionPane.showMessageDialog(null, "Option 4");
                  break;  
           } 

      } 

      public static void main(String[] args) // main program
      {
          new OnlineStore(); // Call out the program.
      }
  }

当我使用扫描仪时,结果正常。

最佳答案

在类内而不是在方法内声明 mSelect 变量。

public class OnlineStore {
int mSelect;
// public static void main(String[] args) // main program

public void display_menu() // Not the main program but the main menu.
{
    String main_selection;

    main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
    mSelect = Integer.parseInt(main_selection);

}

public OnlineStore() // Switch-case program
{
    display_menu(); // Call display menu.
    switch (mSelect) {
        case 1:
            JOptionPane.showMessageDialog(null, "Option 1");
            break;
        case 2:
            JOptionPane.showMessageDialog(null, "Option 2");
            break;
        case 3:
            JOptionPane.showMessageDialog(null, "Option 3");
            break;
        case 4:  // Deliberately not including a default selection.
            JOptionPane.showMessageDialog(null, "Option 4");
            break;
    }

}

public static void main(String[] args) // main program
{
    new OnlineStore(); // Call out the program.
}}

关于java - Joptionpane 不处理 switch-case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31980282/

相关文章:

Java PHP 如何立即检查电子邮件地址是否存在?

java - 从网站角度学习Java的最佳途径或流程

回文的java字符串概念

java - 如何让我的 Jslider 适合我的 JPanel?

c - 使用函数通过 switch 语句传递变量

java - 有人可以看看我的代码并告诉我出了什么问题吗?

java - 为什么 case 语句不带变量?

java - Android JSON 解析(Jackson)

java - 如何最好地管理 Java Swing 弹出表单?

java - 将 JPanel 背景设置为透明