Search code examples
javaswingjoptionpane

JOptionPane.showOptionDialog not working


I am trying to make a little popup game. I have added the story and it all worked perfectly fine, untill i added this piece of code:

    Object[] choices = null;
    Object[] options = { "Caitlyn", "Warwick", "Teemo", "Olaf", "Ashe" };
      int select = JOptionPane.showOptionDialog(null, "Who do you want to fight against?", "Champion selection",
            JOptionPane.CLOSED_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, action
            );

Now, these options appear on every optiondialog that appears. How do I fix this?

This is my code

Object[] choices = null;
    Object[] options = { "Caitlyn", "Warwick", "Teemo", "Olaf", "Ashe" };
      int select = JOptionPane.showOptionDialog(null, "Who do you want to fight against?", "Champion selection",
            JOptionPane.CLOSED_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, action
            );

      switch (select)
        {
        case 0: 
            int cait = 0;
            cait++;
            JOptionPane.showMessageDialog(null, "You will be fighting against the following champion:" + " " + caitlyn.Champion + "  " + "\n She is level" + "  " + caitlyn.Level + " " + "\n And she is a " + caitlyn.Type + "-Champion");

            JOptionPane.showMessageDialog(null, "You have started with 2000HP, Caitlyn has 1700HP.");

            // health

            newchamp.health = 2000;
            caitlyn.enemyhealth = 1700;

            //begin

            Object[] choices1 = null;
            Object[] options1 = { "Criticals", "Pure Damage", "Heal 300 points", "Flee" };
              int action = JOptionPane.showOptionDialog(null, "What will you do?", "action",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, options, options[0]);

            switch (action)
            {
            case 0: 
                int Criticals = (int) (Math.random()*500);
                Criticals++;
                caitlyn.enemyhealth = (caitlyn.enemyhealth-Criticals);
                JOptionPane.showMessageDialog(null, "Caitlyn now has" +" " + caitlyn.enemyhealth + "HP left");
                break;
            case 1:
                int PureDamage = (int) (Math.random()*300);
                PureDamage++;
                caitlyn.enemyhealth = (caitlyn.enemyhealth-PureDamage);
                JOptionPane.showMessageDialog(null, "Caitlyn now has" + " " + caitlyn.enemyhealth + "HP left");
                break;
            case 2:
                int heal = 300;
                heal++;
                newchamp.health = (newchamp.health+heal);
                JOptionPane.showMessageDialog(null,  "You now have"+ " " + newchamp.health + "HP points!");
                break;
            case 3:
                String flee;
                JOptionPane.showMessageDialog(null, "You failed to flee the battle, better luck next round!");
                break;

            }

            JOptionPane.showMessageDialog(null, "It's Caitlyn's turn now!");
            JOptionPane.showMessageDialog(null, "Caitlyn used a damaging attack!");

            int enemyDamage = (int) (Math.random()*350);
            newchamp.health = (newchamp.health - enemyDamage);
            JOptionPane.showMessageDialog(null, "You now have" + " " + newchamp.health +" "+ " HP Left");

            JOptionPane.showMessageDialog(null, "It's your turn again!");


            Object[] choices2 = null;
            Object[] options2 = { "Criticals", "Pure Damage", "Heal 300 points", "Flee" };
              int action1 = JOptionPane.showOptionDialog(null, "What will you do?", "action",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, options, options[0]);

            switch (action1)
            {
            case 0: 
                int Criticals = (int) (Math.random()*700);
                Criticals++;
                caitlyn.enemyhealth = (caitlyn.enemyhealth-Criticals);
                JOptionPane.showMessageDialog(null, "Caitlyn now has" +" " + caitlyn.enemyhealth + "HP left");
                break;
            case 1:
                int PureDamage = (int) (Math.random()*300);
                PureDamage++;
                caitlyn.enemyhealth = (caitlyn.enemyhealth-PureDamage);
                JOptionPane.showMessageDialog(null, "Caitlyn now has" + " " + caitlyn.enemyhealth + "HP left");
                break;
            case 2:
                int heal = 300;
                heal++;
                newchamp.health = (newchamp.health+heal);
                JOptionPane.showMessageDialog(null,  "You now have"+ " " + newchamp.health + "HP points!");
                break;
            case 3:
                String flee;
                JOptionPane.showMessageDialog(null,"You have failed to flee the battle, better luck next round!");
                break;

            }
            JOptionPane.showMessageDialog(null, "It's Caitlyn's turn again!");
            JOptionPane.showMessageDialog(null, "Caitlyn hit a critical!");

            int enemyDamage1 = (int) (Math.random()*650);
            newchamp.health = (newchamp.health - enemyDamage);
            JOptionPane.showMessageDialog(null, "You now have" + " " + newchamp.health +" "+ " HP Left");
            if (caitlyn.enemyhealth < newchamp.health){
                JOptionPane.showMessageDialog(null, "Caitlyn is too injured by your strength to continue this battle. You are victorious!");
            }
            else
                JOptionPane.showMessageDialog(null, "You are too injured to continue this battle. You are defeated.");
            break;
        case 1:
            int war = 0;
            war++;
            JOptionPane.showMessageDialog(null, "Warwick is still being made. This window will now exit the program.");
            break;
        case 2:
            int teem = 0;
            teem++;
            JOptionPane.showMessageDialog(null, "Teemo is still being made. This window will now exit the program.");
            break;
        case 3:
            int ola = 0;
            ola++;
            JOptionPane.showMessageDialog(null, "Olaf is still being made. This window will now exit the program.");
            break;
        case 4:
            int ash = 0;
            ash++;
            JOptionPane.showMessageDialog(null, "Ashe is still being made. This window will now exit the program.");
            break;
        }

    }


}

Solution

  • Did you note this part of your code

    Object[] choices2 = null;
            Object[] options2 = { "Criticals", "Pure Damage", "Heal 300 points", "Flee" };
              int action1 = JOptionPane.showOptionDialog(null, "What will you do?", "action",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, **options**, options[0]);
    

    The ** indicate the probable issue. Shouldn't be options2 ? (there are some other place with the same probable mistake)