简体   繁体   English

Java Swing能量栏未更新

[英]Java Swing energybar not updating

Was wondering if any of you could help me with this: 想知道你们中的任何一个可以帮助我吗:

I have a UIMain class with the following methods. 我有一个具有以下方法的UIMain类。 My timer doesn't appear to be working whenever I call all 3 in the main method 每当我在main方法中调用所有3个计时器时,我的计时器似乎都无法工作

setupEnergyBar();
setupDeductingEnergyBar();
updateData();

public void setupEnergyBar(){
    energyBar = new JProgressBar();
    energyBar.setStringPainted(true);
    energyBar.setMinimum(0);
    energyBar.setMaximum(100);
    setupDeductingEnergyBar();
}

public void setupDeductingEnergyBar(){
    energyDeductTimer = new Timer(1000, new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            character.setCharEnergyLvl(character.getCharEnergyLvl() - 5);
    }
    });
    energyDeductTimer.start();
    updateData();

}

public void updateData(){
        character.getCharName();
        character.getCharEnergyLvl();
        energyBar.setValue(character.getCharEnergyLvl());
//          setCharStatus();
//          setCharActionLvl(getCharActionLvl());
//          setCharEnergyLvl(getCharEnergyLvl());
//          setCoins(getCoins());
//          setHero(isHero());
//          setCrimesFought(getCrimesFought());
//          setCrimesCommited(getCrimesCommited());
//          setCrimesIgnored(getCrimesIgnored());
//          
    }
}

Many thanks 非常感谢

Your problem is in the setupDeductingEnergyBar method. 您的问题出在setupDeductingEnergyBar方法中。 You are creating a timer, and updating the characters energy, but you are not updating the progress bar. 您正在创建一个计时器,并更新角色能量,但没有更新进度条。 The fix is just to move the updateData method into the energyDeductTimer 's action listener. 解决方法只是将updateData方法移到energyDeductTimer的动作侦听器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM