我制作了一个选项面板,但我无法让按钮真正按预期运行。 我试图将 action 设置为连接到面板的 int,但这不起作用。
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;
另外,我不知道如何才能使已经造成的伤害或治疗得以保存,并在下一次攻击时保持该水平,而不是恢复为默认值。
最佳答案
我认为你需要:
int action = JOptionPane.showOptionDialog(null, "What will you do?", "action",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
您忽略了该方法的返回值,它告诉您按下了哪个按钮。
关于java - 来自 JOptionPane 的返回值表示选择的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15003170/