2013-02-21 40 views
1

我已經制作了一個選項面板,但是我無法按照他們的意思去實際執行按鈕。 我試圖讓行動一個int連接到面板,但這是行不通的。JOptionPane的返回值表示所選按鈕

Object[] choices = null; 
    Object[] options = { "Criticals", "Pure Damage", "Heal 300 points", "Flee" }; 
    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; 

也,我不知道我將如何讓這個傷害或治療已經完成,被保存並會留在該水平下一次攻擊,而不是恢復到默認值。

+0

什麼是變量動作?它在哪裏定義? – 2013-02-21 13:16:06

+0

在我的完整代碼的最頂端: private static int action; – 2013-02-21 13:16:39

+0

你沒有得到'showConfirmDialog()'的返回值並將它賦給'action' – 2013-02-21 13:20:32

回答

3

我想你需要:

int action = JOptionPane.showOptionDialog(null, "What will you do?", "action", 
     JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, 
     null, options, options[0]); 

你忽略的方法,它告訴你按下哪個按鈕的返回值。

+0

非常感謝! :) – 2013-02-21 13:19:01

+0

當你有機會時,請[接受](http://meta.stackexchange.com/a/65088/155831)答案。 – 2013-02-21 13:27:17