Frage

Im trying to print the top 10 People's score and their names along side.

My Declerations:

private JLabel JSingleplayer [];
private JLabel JSingleScore [];
private String [] PlayerName = {"----------","----------","----------","----------","----------","----------","----------","----------","----------","----------"};
private String [] PlayerScore = {"--","--","--","--","--","--","--","--","--","--",};

And trying to print it all here:

for (int x = 0; x < 10; x++)
{
    JSingleplayer[x].setText(Singleplayer[x]);
    JSingleplayer[x] = new JLabel(Singleplayer[x]);
    EndPanelscore.add(JSingleplayer[x]);
    JSingleScore[x].setText(SingleScore[x]);
    EndPanelscore.add(JSingleScore[x]);
}
EndFrame.add(EndPanelscore);

Im getting a big error when i add the for loop, it doesn't allow the array to be printed.

If you have a solution or a better way to code that part (working) Please help me in the way i coded it. Will rate 5, like, all that good stuff.

War es hilfreich?

Lösung

You've got a few problems.

First, in Java you need to declare the length of the array before assigning values to the array.

Second, your code was attempting to set the text of the label before the label was assigned to the array.

Third, you were using a variable that didn't exist (Singleplayer).

Try the following changes and see if you can go from there...

private JLabel JSingleplayer [] = new JLabel[10];

for (int x = 0; x < 10; x++)
{
    JSingleplayer[x] = new JLabel(PlayerName[x]);
    // the following assumes you've also declared EndPanelscore...
    EndPanelscore.add(JSingleplayer[x]);
    // you have similar problems here...
    //JSingleScore[x].setText(SingleScore[x]);
    //EndPanelscore.add(JSingleScore[x]);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top