DK_
DK_

Reputation: 147

I need to display a score in my game that can update as the player gains score

I've been trying to get this to work for a long time and I'm stuck. I've got a scoring system that "works" (it has a score that will increase when the player collects a coin) But I cannot for the life of me get it to display the score in the top left of the screen (or get it to display at all). I've tried using a JLabel like so:

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

Upvotes: 0

Views: 7223

Answers (1)

Alya'a Gamal
Alya'a Gamal

Reputation: 5638

You need to put the label inside you panel , like this example :

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);
}

Upvotes: 1

Related Questions