Java 从JOptionPane返回值,指示所选按钮
我已经做了一个选项面板,但我不能让按钮真正按照他们的意图去做。 我曾尝试将动作设置为连接到面板的int,但这不起作用Java 从JOptionPane返回值,指示所选按钮,java,switch-statement,joptionpane,Java,Switch Statement,Joptionpane,我已经做了一个选项面板,但我不能让按钮真正按照他们的意图去做。 我曾尝试将动作设置为连接到面板的int,但这不起作用 Object[] choices = null; Object[] options = { "Criticals", "Pure Damage", "Heal 300 points", "Flee" }; JOptionPane.showOptionDialog(null, "What will you do?", "action", JO
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]);
您忽略了方法的返回值,该值告诉您按下了哪个按钮。变量操作是什么?它定义在哪里?在我完整代码的最顶端:私有静态int操作;您没有获得
showConfirmDialog()
的返回值并将其分配给操作
请在有机会时提供答案。