我需要在我的游戏中显示一个分数,可以在玩家获得分数时更新

时间:2014-02-06 10:06:18

标签: java scoring

我一直试图让这个工作很长时间而且我被困住了。我有一个“有效”的得分系统(当玩家收集硬币时它的分数会增加)但我不能为我的生活让它在屏幕的左上角显示得分(或得到)它完全显示)。我尝试过像这样使用JLabel:

JLabel scoreLabel = new JLabel("Score: 0");
public void someoneScored()
{
Score++;
scoreLabel.setBounds(5, 5, WIDTH, HEIGHT);
    scoreLabel.setText("Score: " + Score);
}

1 个答案:

答案 0 :(得分:1)

您需要将label放在panel内,如下例所示:

JLabel scoreLabel = new JLabel("Score: 0");
add(scoreLabel);/// assuming you have Jpanel class

public void someoneScored()
{
  Score++;
  scoreLabel.setBounds(5, 5, WIDTH, HEIGHT);
  scoreLabel.setText("Score: " + Score);
}