gpt4 book ai didi

java - JProgressBar 在使用 setValue(newValue) 时不更新值

转载 作者:行者123 更新时间:2023-11-30 10:35:42 28 4
gpt4 key购买 nike

我正在使用 JProgressBar 作为健康条,所以我想随着游戏的进行手动设置条的最大值和值。该游戏是一款口袋妖怪游戏,它允许召唤新的口袋妖怪。这些新的 Pokemon 可以有不同的最大健康值和当前健康值,这就是我调用 progressBar.setMaximum(newPokemonMaxHealth)progressBar.setValue(currentPokemonHealth) 的原因。我什至调用了 progressBar.repaint()progressBar.revalidate()progressBar.update(progressBar.getGraphics(),但都没有这些对我有用。

这里是进度条的初始化:

    UIManager.put("ProgressBar.selectionForeground", Color.BLACK);
poke1Health = new JProgressBar(0);
poke1Health.setMaximum(board.getTrainer1().getBattlingPokemon().getMaxHealth());
poke1Health.setValue(board.getTrainer1().getBattlingPokemon().getCurrentHealth());

poke1Health.setStringPainted(true);
poke1Health.setForeground(Color.green);
poke1Health.setBackground(Color.red);
poke1Health.setPreferredSize(new Dimension(50, 10));
centerPanel1.add(poke1Health);

然后当我尝试更改最大值和值时:

            board.getTrainer1().pokemonChanged(1);
poke1Health.setValue(board.getTrainer1().getPokemon(0).getCurrentHealth());
poke1Health.setMaximum(board.getTrainer1().getPokemon(0).getMaxHealth());
poke1Health.repaint();
poke1Health.revalidate();
poke1Health.update(poke1Health.getGraphics());

我打印出不同口袋妖怪的当前健康值和最大健康值,这些值是它们应该的值,这表明值不是问题。

出于某种原因,bars 值似乎不想更改,但最大值确实如此。

当新的宝可梦生命值高于之前的宝可梦生命值时,不会更新数值。如果新的口袋妖怪健康低于以前的口袋妖怪健康,它会更新值,没问题。这是我打印的内容:

Pokemon 1 Max Health: 49

Pokemon 2 Max Health: 50

Pokemon 1 Current Health: 49

Pokemon 2 Current Health: 50

然后我在设置新的最大值和新值之前和之后打印条值。

Bars value before: 49

Bars value after: 49

Bars Max before: 49

Bars Max after: 50

由于某种原因,最大值发生了变化,但该值没有变化。我为获取 Pokemon 2 当前健康状况所做的打印输出与我用来尝试设置栏的新值的代码完全相同。这里:

 poke1Health.setValue(board.getTrainer1().getBattlingPokemon().getCurrentHealth());

System.out.println("Pokemon 2 Current Health: " + board.getTrainer1().getBattlingPokemon().getCurrentHealth());

请帮帮我!找不到进度条值不想更新的原因!!!

谢谢!

最佳答案

事实证明,如果您的新值高于条形最大值,则不会设置新值。您必须先更新最大值,然后设置等于或小于最大值的新值。相反,我将 setMaximum 放在 setValue 之前,一切正常!

关于java - JProgressBar 在使用 setValue(newValue) 时不更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985199/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com