简体   繁体   中英

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. My timer doesn't appear to be working whenever I call all 3 in the main method

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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