简体   繁体   中英

JLabel not showing

I'm in the process of finishing up a game and I've reached a point that has made me extremely frustrated. It's probably a simple fix and something I'm overlooking, so maybe you guys can help me out. I'm trying to get a series of 5 JLabels to show up on screen with the 5 high scores from the game. However, it keeps showing up with the background and no labels. The following is my code:

import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EndPanel extends JPanel{

   GameGUI gui;
   JLabel[] array;

   public EndPanel(GameGUI gui) {
       super();
       this.gui = gui;
       array = new JLabel[5];
       for(JLabel i : array) {
          i = new JLabel("");
          i.setVisible(true);
          this.add(i);
       }
       int i = 0;
       for(HighScore h: gui.getGameDriver().getHighScores()) {
          System.out.println(gui.getGameDriver().getHighScores());
          array[i].setText(h.toString());
          i++;
       }

    }

    public void paint(Graphics g) {
       super.paint(g);
       g.drawImage(gui.images.get(32), 0, 0, this);
       g.drawImage(gui.images.get(31), 112, 100, this);
    }
}

The initialization of the array is strongly suspicious. Instead of:

   for(JLabel i : array) {
      i = new JLabel("");
      i.setVisible(true);
      this.add(i);
   }

try:

   for (int i = 0; i < array.length; i++) {
      array[i] = new JLabel("");
      array[i].setVisible(true);
      add(array[i]);
   }

Untested as your code cannot be compiled.

[EDIT] Actually, you don't need to create an array of JLabel since you throw them out (array goes out of scope at the end of the method). You can create the labels on the fly:

   for (HighScore h : gui.getGameDriver().getHighScores()) {
      System.out.println(gui.getGameDriver().getHighScores());
      JLabel label = new JLabel(h.toString());
      add(label);
   }

You still need to set the position of these labels, with a layout manager or by absolute position.

Instead of using for each loop try using regular for loop, like this:

JLabel [] label = new JLabel[5];
    for(int i = 0; i<5;i++){
        label[i] = new JLabel("Label number: " +i);
        panel.add(label[i]);
    }

This should work.

Since you do not use layout manager you have set the label position and size yourself. Otherwise I guess all your labels have size=0 and are positioned at point 0,0.

BTW, do you have a special reason not to use layout manager or you just not aware on it?

try moving the Jlabel initialization code inside the loop of high score :

   int i = 0;
   for(HighScore h: gui.getGameDriver().getHighScores()) {
      System.out.println(gui.getGameDriver().getHighScores());
      array[i] = new JLabel(h.toString());
      this.add(array[i]);
      i++;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM