JLabel 不更新得分

JLabel not updating points scored

我正尝试在 Greenfoot 中制作一个简单的游戏。我的其他一切都很好,但是,它没有更新要点。

int pointsScored = 0;
JFrame frame = new JFrame("Points Scored");
JLabel label = new JLabel("Points Scored " + pointsScored);

public void act() 
{
    label.setPreferredSize(new Dimension(100, 100));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    if (atWorldEdge()) {
       turn(180);
       pointsScored++;
       if (pointsScored != 0) {
           frame.setVisible(true);
        }
    }
    move();
}    

要更改标签中的文本,您需要使用:

pointsScored++;
label.setText( "Points Scored " + pointsScored );

更改变量的值不会更新以前使用该变量的任何其他表达式。

每次得分变化时更新 JLabels 文本

pointsScored++;
label.setText("Points Scored: " + pointsScored);

只更新变量不会更新标签