简体   繁体   English

图像未显示在JLabel上

[英]Image not showing on JLabel

I've been tasked to make a java replica of Candy Crush Saga. 我的任务是制作Candy Crush Saga的java复制品。 Right now im quite stuck on the GUI part. 现在我非常坚持GUI部分。 I've decided that each candy will be represented by a JLabel holding the candy icon, and a mouselistener to control the functionality. 我已经决定每个糖果将由一个拿着糖果图标的JLabel和一个控制功能的mouselistener来代表。 What happens is, after i finish running the screen shows, the mouse listeners respond but the image doesn't show, meaning i can press the labels get a response but cannot see the icons. 发生的事情是,在我完成屏幕显示后,鼠标监听器会响应,但图像没有显示,这意味着我可以按标签获得响应,但无法看到图标。 I take this as the labels are on the panel but somehow not visible or the icon is not loaded correctly - although when checking the ImageIcon.toString it shows the path to the file. 我拿这个,因为标签在面板上,但不知何故不可见或图标未正确加载 - 虽然在检查ImageIcon.toString时它显示文件的路径。 Any ideas? 有任何想法吗?

Here is the code: 这是代码:

public class Board extends JPanel {
Candy[][] board;
static final int TILE_SIZE = 55;
static final int  TILES_MARGIN = 8;


public Board() {
    setFocusable(true);

    board = new Candy[13][13];
    Candy c;
    for (int i = 0; i < 13; i++)
        for (int j = 0; j < 13; j++) {
            if (i != 0 && i != 1 && j != 0 && j != 1 && i != 11 && i != 12 && j != 11 && j != 12) {
                Random rand = new Random();
                int randomNum = rand.nextInt((6 - 1) + 1) + 1;
                c = new Basic(randomNum, this);
            } else {
                c = new Basic(0, this);
            }
            setAt(i, j, c);
        }
    repaint();
}

public void drawCandy(Graphics g2, Candy candy, int x, int y) {
    Graphics2D g = ((Graphics2D) g2);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    int value = candy.getClr();
    int xOffset = offsetCoors(x);
    int yOffset = offsetCoors(y);
    ImageIcon myImg = candy.switchIcon();
    JLabel toAdd = new JLabel(myImg);
    toAdd.setIcon(myImg);
    toAdd.setLocation(xOffset,yOffset);
    toAdd.setSize(TILE_SIZE,TILE_SIZE);
    toAdd.addMouseListener(new ButtonPressed(x,y,candy));
    toAdd.setVisible(true);
    if (value != 0)
        add(toAdd);
}
private static int offsetCoors(int arg) {
    return (arg-2) * (TILES_MARGIN + TILE_SIZE) + TILES_MARGIN;
}

public void paint(Graphics g) {
    super.paint(g);
    removeAll();
    requestFocusInWindow();
    g.setColor(Color.black);
    g.fillRect(0, 0, this.getSize().width, this.getSize().height);

    for (int x = 2; x < 11; x++) {
        for (int y = 2; y < 11; y++) {
            drawCandy(g, board[x][y], x, y);
        }
    }

    validate();
}

and the JFrame : 和JFrame:

public Game() {
    super("Candy Crush Game");
    setDefaultLookAndFeelDecorated(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    setSize(600, 600);
    setResizable(false);
    setLocationRelativeTo(null);
    this.board = new Board();


    this.score = 0;
    this.moves = 20;

    this.getContentPane().add(board, BorderLayout.CENTER);
    setVisible(true);


     board.checkSquare(2, 2, 10, 10);


}

I'm quite frustrated, any help will be great! 我很沮丧,任何帮助都会很棒!

Instead of overriding paint() method use paintComponent() method for JPanel . 而不是覆盖paint()方法而是使用paintComponent()方法为JPanel

@Overrie
public void paintComponent(Graphics g) {
     super.paintComponent(g);
     //your custom painting here
}

Read more 阅读更多


There might be some issue in reading image icon. 阅读图像图标可能存在一些问题。 My another post might help you. 我的另一篇文章可能对你有帮助

Instead of creating new JLabel simply change it's icon. 而不是创建新的JLabel只需更改它的图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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