【问题标题】:I need to display a score in my game that can update as the player gains score我需要在我的游戏中显示一个分数,该分数可以随着玩家获得分数而更新
【发布时间】:2014-03-03 04:56:02
【问题描述】:

我一直试图让它工作很长时间,但我被卡住了。我有一个“有效”的评分系统(当玩家收集硬币时,它的分数会增加)但我一辈子都无法让它在屏幕的左上角显示分数(或者得到它完全显示)。我试过像这样使用 JLabel:

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

【问题讨论】:

  • 为什么不用 g.drawString(Score, x ,y);?
  • 将您的 JLabel 添加到当前显示的容器中。

标签: java scoring


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 2020-12-31
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多