First of all, I would recommend to take a look at how to use JPanels and other GUI components in Swing. You could use a JPanel to be added to the contentPane
of your JFrame
. Add a desired LayoutManager that is fitting for your need and use it to position a JLabel according to your wishes.
When you call your showScore()
method, use setText(String text)
on a JLabel
instead of the int score
that you supply as parameter.
In order to set the text using a String, you need to transform your int score
. You can do either of the following two things (but use the first option):
label.setText(String.valueOf(score));
label.setText("" + score);
If you want the score label to appear or disappear, add/ remove it from the JPanel
that you added to the JFrame
. After adding it, call setText()
on the label to display the score. Then, since you reset the text of the label, you should call repaint()
on the JPanel
. If you choose to add/ remove the JLabel
instead of just changing its text you should also call revalidate()
on the JPanel
.
So all in all the function should look something like this (this is incomplete), assuming totalHits
is actually your score.
// declare instance fields before constructor
// both variables are reachable by showScore()
private JPanel scorePanel;
private JLabel scoreLabel;
// in constructor of your class
scorePanel = new JPanel(); // initialize the JPanel
scorePanel.setLayout(new BorderLayout()); // give it a layout
scoreLabel = new JLabel(); // initialize the label
frame.add(scorePanel); // add JPanel to your JFrame
// method obviously outside constructor
public void showScore(String username, int score)
{
// clear the JPanel that contains the JLabel to remove it
scorePanel.removeAll();
scoreLabel.setText("<html>" + username + "<br/>Total Hits: " + String.valueOf(score) + "</html>");
scorePanel.add(label, BorderLayout.CENTER);
scorePanel.revalidate();
scorePanel.repaint();
}
You could also declare and initialize the JPanel
and JLabel
in the showScore()
method, making it more local. But then you would also need to call pack()
on the JFrame
and clear the previous JPanel
from it every time you call the function.
.