将在运行时更改的更改更新为窗口中显示的对象值

时间:2012-05-15 08:06:50

标签: user-interface refresh

我刚刚开始使用java和练习我一直试图组建一个小型RPG游戏。我最近成功设置了一个ui,但是我在使窗口显示不同actor对象变量的变化方面遇到了很多麻烦。例如,当我点击“攻击”时,我攻击的敌人的HP降低了一定量。我希望这个改变能反映在JLabel中,在窗口中显示敌人的HP,但我一直无法做到这一点。我做了一些研究,发现类似的问题建议使用invalidate(),validate()和repaint(),但我无法让它们为我工作。有人可以提供任何建议吗?非常感谢。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class SimpleRPGBattleUI extends JFrame 
{ 
// Create player and enemy actors (array size 4 for convenience's sake; number actors 1-3)
static actor[] player = new actor[4];
static actor[] enemy  = new actor[4];

static boolean playerTurnTaken = false;

// Actions available to current player character
//Align towards the left of window, stacked vertically
JButton jbtAttack    = new JButton("Attack");
JButton jbtSkills    = new JButton("Skills");
JButton jbtMagic     = new JButton("Magic");
JButton jbtInventory = new JButton("Inventory");

public SimpleRPGBattleUI()
{   
// Displays HP, MP, SP, and status of actor
    //Implement as an object that can be instantiated multiple times into the window,
JLabel jbt1StatusName   = new JLabel(player[1].name);
JLabel jbt1StatusHP     = new JLabel("HP: " + player[1].HP);
JLabel jbt1StatusMP     = new JLabel("MP: " + player[1].MP);
JLabel jbt1StatusSP     = new JLabel("SP: " + player[1].SP);
JLabel jbt1StatusEffects    = new JLabel("Poisoned");

JLabel jbt2StatusName   = new JLabel(player[2].name);
JLabel jbt2StatusHP     = new JLabel("HP: " + player[2].HP);
JLabel jbt2StatusMP     = new JLabel("MP: " + player[2].MP);
JLabel jbt2StatusSP     = new JLabel("SP: " + player[2].SP);
JLabel jbt2StatusEffects    = new JLabel("Blinded");

JLabel jbt3StatusName   = new JLabel(player[3].name);
JLabel jbt3StatusHP     = new JLabel("HP: " + player[3].HP);
JLabel jbt3StatusMP     = new JLabel("MP: " + player[3].MP);
JLabel jbt3StatusSP     = new JLabel("SP: " + player[3].SP);
JLabel jbt3StatusEffects    = new JLabel("OK");

JLabel jbt4StatusName   = new JLabel(enemy[1].name);
JLabel jbt4StatusHP     = new JLabel("HP: " + enemy[1].HP);
JLabel jbt4StatusMP     = new JLabel("MP: " + enemy[1].MP);
JLabel jbt4StatusSP     = new JLabel("SP: " + enemy[1].SP);
JLabel jbt4StatusEffects    = new JLabel("OK");

JLabel jbt5StatusName   = new JLabel(enemy[2].name);
JLabel jbt5StatusHP     = new JLabel("HP: " + enemy[2].HP);
JLabel jbt5StatusMP     = new JLabel("MP: " + enemy[2].MP);
JLabel jbt5StatusSP     = new JLabel("SP: " + enemy[2].SP);
JLabel jbt5StatusEffects    = new JLabel("Shielded");

JLabel jbt6StatusName   = new JLabel(enemy[3].name);
JLabel jbt6StatusHP     = new JLabel("HP: " + enemy[3].HP);
JLabel jbt6StatusMP     = new JLabel("MP: " + enemy[3].MP);
JLabel jbt6StatusSP     = new JLabel("SP: " + enemy[3].SP);
JLabel jbt6StatusEffects    = new JLabel("OK");

JPanel pStatusField1 = new JPanel();
    pStatusField1.setLayout(new GridLayout(5, 1));
    pStatusField1.add(jbt1StatusName);
    pStatusField1.add(jbt1StatusHP);
    pStatusField1.add(jbt1StatusMP);
    pStatusField1.add(jbt1StatusSP);
    pStatusField1.add(jbt1StatusEffects);
JPanel pStatusField2 = new JPanel();
    pStatusField2.setLayout(new GridLayout(5, 1));
    pStatusField2.add(jbt2StatusName);
    pStatusField2.add(jbt2StatusHP);
    pStatusField2.add(jbt2StatusMP);
    pStatusField2.add(jbt2StatusSP);
    pStatusField2.add(jbt2StatusEffects);
JPanel pStatusField3 = new JPanel();
    pStatusField3.setLayout(new GridLayout(5, 1));
    pStatusField3.add(jbt3StatusName);
    pStatusField3.add(jbt3StatusHP);
    pStatusField3.add(jbt3StatusMP);
    pStatusField3.add(jbt3StatusSP);
    pStatusField3.add(jbt3StatusEffects);

JPanel pStatusField4 = new JPanel();
    pStatusField4.setLayout(new GridLayout(5, 1));
    pStatusField4.add(jbt4StatusName);
    pStatusField4.add(jbt4StatusHP);
    pStatusField4.add(jbt4StatusMP);
    pStatusField4.add(jbt4StatusSP);
    pStatusField4.add(jbt4StatusEffects);
JPanel pStatusField5 = new JPanel();
    pStatusField5.setLayout(new GridLayout(5, 1));
    pStatusField5.add(jbt5StatusName);
    pStatusField5.add(jbt5StatusHP);
    pStatusField5.add(jbt5StatusMP);
    pStatusField5.add(jbt5StatusSP);
    pStatusField5.add(jbt5StatusEffects);
JPanel pStatusField6 = new JPanel();
    pStatusField6.setLayout(new GridLayout(5, 1));
    pStatusField6.add(jbt6StatusName);
    pStatusField6.add(jbt6StatusHP);
    pStatusField6.add(jbt6StatusMP);
    pStatusField6.add(jbt6StatusSP);
    pStatusField6.add(jbt6StatusEffects);

JPanel pStatusFieldPlayer    = new JPanel();
    pStatusFieldPlayer.setLayout(new FlowLayout());
    pStatusFieldPlayer.add(pStatusField1);
    pStatusFieldPlayer.add(pStatusField2);
    pStatusFieldPlayer.add(pStatusField3);

JPanel pStatusFieldEnemy     = new JPanel();
    pStatusFieldEnemy.add(pStatusField4);
    pStatusFieldEnemy.add(pStatusField5);
    pStatusFieldEnemy.add(pStatusField6);

JPanel pActions   = new JPanel();
    pActions.setLayout(new GridLayout(4, 1));
    pActions.add(jbtAttack);
    pActions.add(jbtSkills);
    pActions.add(jbtMagic);
    pActions.add(jbtInventory);

JPanel pNarration = new JPanel();
    pNarration.add(new JTextArea("The knight dealt 15 points of damage to the Necro."),
            BorderLayout.SOUTH);
JPanel pHolder    = new JPanel();
    pHolder.add(pActions, BorderLayout.WEST);
JPanel pHolder2   = new JPanel();
    pHolder2.setLayout(new GridLayout(2, 1));
    pHolder2.add(pStatusFieldPlayer, BorderLayout.NORTH);
    pHolder2.add(pStatusFieldEnemy, BorderLayout.SOUTH);

// add contents into frame
add(pHolder, BorderLayout.WEST);
add(pHolder2);
add(pNarration, BorderLayout.SOUTH);


jbtAttack.addActionListener(new Listener());
jbtSkills.addActionListener(new Listener());
jbtMagic.addActionListener(new Listener());
jbtInventory.addActionListener(new Listener());
}   

class Listener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == jbtAttack)
        {
            System.out.println("Attack");
            System.out.println("Old enemy HP: " + enemy[1].HP);
            actor.attack(player[1], enemy[1]);
            System.out.println("New enemy HP: " + enemy[1].HP);
            playerTurnTaken = true;

        }
        else if(e.getSource() == jbtSkills)
        {
            System.out.println("Skill");
            System.out.println("Old enemy HP: " + enemy[1].HP);
            actor.deadlyBlow(player[1], enemy[1]);
            System.out.println("New enemy HP: " + enemy[1].HP);
            playerTurnTaken = true;
        }
        else if(e.getSource() == jbtMagic)
        {
            System.out.println("Magic");
        }
        else if(e.getSource() == jbtInventory)
        {
            System.out.println("Inventory");
        }
    }
}

public static void main(String[] args)
{
    String input = "";
    int action = 0;

    JOptionPane.showMessageDialog(null, "Which three warriors go forth?");
    for(int i = 1; i < player.length; i++)
    {
        input = JOptionPane.showInputDialog(null, "1: Knight\n2: Mage\n3: Rogue");
        action  = Integer.parseInt(input);
        player[i] = new actor();
        player[i].setPlayerClass(action);
    }

    Random rand    = new Random();
    for(int i = 1; i < enemy.length; i++)
    {
        action   = rand.nextInt(3)+1;
        enemy[i] = new actor();
        enemy[i].setEnemyClass(action);
    }

    SimpleRPGBattleUI frame = new SimpleRPGBattleUI();
    frame.setTitle("Battle");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true); 

    while((player[1].HP > 0) && (enemy[1].HP > 0))
    { 
        if(playerTurnTaken)
        {
        actor.attack(enemy[1], player[1]);
        System.out.println("Player HP: " + player[1].HP + "\nEnemy HP: " + enemy[1].HP);
        playerTurnTaken = false;
        }
        frame.invalidate(); 
        frame.validate(); 
        frame.repaint(); 
    }
}

}

1 个答案:

答案 0 :(得分:0)

我设法找到了我需要的帮助: Swing JLabel text change on the running application

由于这里提供的解决方案,我已经开始着手解决问题并继续前进。非常感谢!