Hi, I'm trying to create a highscore database for my game, and would like to start off with a view high score frame. When the user clicks this frame will pop up, but for some reason I can't set the player and score information to my JLabels.
I'm not sure if I should use 2d array instead. Beginner here.
thanks
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;
public class HighScoreManager extends JFrame {
HighScore[] scores;
int scoreCount, scorePosition;
Container hiS;
JLabel lblTitle, lblFirst, lblFirstPlayer, lblFirstScore,
lblSecond, lblSecondPlayer, lblSecondScore,
lblThird, lblThirdPlayer, lblThirdScore,
lblFourth, lblFourthPlayer, lblFourthScore,
lblFifth, lblFifthPlayer, lblFifthScore;
JPanel northPanel, centerPanel;
public HighScoreManager() {
hiS = this.getContentPane();
hiS.setLayout(new BorderLayout());
northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
lblTitle = new JLabel("Hi-Scores");
northPanel.add(lblTitle);
hiS.add(northPanel, BorderLayout.NORTH);
//Center panel
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(5, 3));
lblFirst = new JLabel("1.");
lblFirstPlayer = new JLabel("");
lblFirstScore = new JLabel("");
lblSecond = new JLabel("2.");
lblSecondPlayer = new JLabel("");
lblSecondScore = new JLabel("");
lblThird = new JLabel("3.");
lblThirdPlayer = new JLabel("");
lblThirdScore = new JLabel("");
lblFourth = new JLabel("4.");
lblFourthPlayer = new JLabel("");
lblFourthScore = new JLabel("");
lblFifth = new JLabel("5.");
lblFifthPlayer = new JLabel("");
lblFifthScore = new JLabel("");
centerPanel.add(lblFirst);
centerPanel.add(lblFirstPlayer);
centerPanel.add(lblFirstScore);
centerPanel.add(lblSecond);
centerPanel.add(lblSecondPlayer);
centerPanel.add(lblSecondScore);
centerPanel.add(lblThird);
centerPanel.add(lblThirdPlayer);
centerPanel.add(lblThirdScore);
centerPanel.add(lblFourth);
centerPanel.add(lblFourthPlayer);
centerPanel.add(lblFourthScore);
centerPanel.add(lblFifth);
centerPanel.add(lblFifthPlayer);
centerPanel.add(lblFifthScore);
hiS.add(centerPanel, BorderLayout.CENTER);
//
scores = new HighScore[5];
scoreCount = 0;
try {
File file = new File("hiScore.txt");
Scanner input = new Scanner(file);
// Iterate through the data in the data file and create objects
while(input.hasNext()) {
String player = input.next();
int score = input.nextInt();
System.out.println(player + score);
scores[scoreCount] = new HighScore(player, score);
scoreCount++; // increment the value because we actually created a car
System.out.println(scoreCount);
}
input.close(); // close the input stream as we have finished reading the data
// set the position to be zero
scorePosition = 0;
showScore();
}
catch(Exception e) {
//JOptionPane.showMessageDialog(this, "File Could Not be Opened");
}
}
public void showScore() {
// pupulate the text boxes with the values of the first car in the array.
if(scoreCount != -1) {
lblFirstPlayer.setText( scores[scorePosition].getPlayer() );
}
else {
lblFirstPlayer.setText( scores[scorePosition].getPlayer() );
lblFirstScore.setText("");
}
}