从JOptionPane返回的值,指示已选择的按钮 [英] Return Value from JOptionPane indicating the button selected

查看:72
本文介绍了从JOptionPane返回的值,指示已选择的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个选项面板,但是我无法让按钮真正按其意图执行. 我试图将action设置为与面板连接的int,但这是行不通的.

I have made an option panel, but I can't get the buttons to actually do as they are meant to. I have tried to make action an int which is connected to the panel, but that doesn't work.

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;

另外,我不知道如何保存或保存已经造成的伤害或治愈,并且在下次攻击时将保持在该水平,而不是恢复默认值.

also, I don't know how I would make it so that the damage or the heal that has been done, is saved and will stay at that level for the next attack, instead of return to default.

推荐答案

我认为您需要:

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

您将忽略该方法的返回值,该方法将告诉您按下了哪个按钮.

You're ignoring the return value of the method, which tells you which button is pressed.

这篇关于从JOptionPane返回的值,指示已选择的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆