我在 ImageIcon 数组中声明了 30 个 ImageIcon。我已经使用 FOR 循环声明了它们。唯一的问题是,当我尝试将 ImageIcon 添加到 JLabel 并在屏幕上显示 JPanel 时,它不起作用。这是我的代码:
package screens;
import javax.swing.*;
import java.awt.*;
public class gameScreen extends JPanel {
private static final long serialVersionUID = 1L;
// -------------VARIABLES---------------//
Image wallpaper = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/wallpaper.jpg"));
ImageIcon[] teamsImg = new ImageIcon[30];
public gameScreen() {
for(int i = 0;i>30;i++){
teamsImg[i] = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
getClass().getResource("images/img.png")));
}
JLabel label = new JLabel(teamsImg[27]);
add(label);
}
// -------------PAINT FUNCTION----------//
public void paintComponent(Graphics g) {
g.drawImage(wallpaper,0,0,null);
}
}