1

オプションパネルを作成しましたが、ボタンを実際に機能させることができません。パネルに接続されている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;

また、ダメージやヒールが保存され、デフォルトに戻るのではなく、次の攻撃のためにそのレベルに留まるようにする方法がわかりません。

4

1 に答える 1

3

私はあなたが必要だと思います:

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

どのボタンが押されたかを示すメソッドの戻り値を無視しています。

于 2013-02-21T13:17:25.533 に答える