2013-02-21 25 views
0

我想做一個小小的彈出式遊戲。 我已經加入的故事和它的所有工作完全正常,直到我加入這一段代碼:JOptionPane.showOptionDialog不起作用

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

現在,這些選項會出現在該出現的每optiondialog。我該如何解決?

這是我的代碼

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

    } 


} 
+0

OOT但我的建議是總是通過父組件(窗口)作爲第一個參數'JOptionPane.showOptionDialog()'。否則,您可能會在某些系統或多顯示器環境中遇到奇怪的問題 – 2013-02-21 14:01:32

+0

是稍後添加到JComboBox或JCheckBox的那些選項???,爲了更快地發佈[SSCCE](http://sscce.org/)可運行,可編譯 – mKorbel 2013-02-21 14:02:48

+0

這是非常奇怪的,我用你的代碼,並添加了幾行來給你的應用程序類似的行爲,它工作正常。也許正如第一條評論所述,您需要指定將用於對話框的窗口,或嘗試在顯示新消息對話框之前使選項數組無效。 – 2013-02-21 14:08:45

回答

1

你注意你的代碼

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]); 

,這部分的**表示可能的問題。不應該是options2? (有一些其他地方有相同的可能錯誤)

+1

我相信OP抱怨意想不到的輸出,表明該程序正在編譯(雖然他可能沒有顯示所有相關的代碼) – Ozzy 2013-02-21 14:14:11

+0

謝謝ben,我忽略了 – 2013-02-21 14:34:48