ImageIcon 数组未使用我的图像填充按钮数组

ImageIcon array isn't populating button array with my Images

所以我在这里制作了一个带有按钮的 Grid 4x4 网格:

import java.awt.GridLayout;

import javax.swing.JPanel;

public class GameGrid extends JFrame {

JPanel pan = new JPanel();
ShapeButtons[] buttons = new ShapeButtons[16];


public GameGrid(){
    super("Shape Match");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pan.setLayout(new GridLayout(4, 4 ,2 ,2));
    setResizable(true);
    setSize(500,500);
    for (int i = 0; i<16; i++){

        buttons[i] = new ShapeButtons();
        pan.add(buttons[i]);

    }

    add(pan);
    setVisible(true);

}

public static void main(String[] args){

    new GameGrid();
}

}

现在我创建了一个单独的 class 作为图像的存储库:

 import javax.swing.ImageIcon;
 import javax.swing.JButton;

public class ShapeButtons extends JButton{
ImageIcon[] Shapes = new ImageIcon[8];

public ShapeButtons(){


    Shapes[0] = new ImageIcon("src/images/Circle.jpg");
    Shapes[1] = new ImageIcon("src/images/Diamond.jpg");
    Shapes[2] = new ImageIcon("src/images/Square.jpg");
    Shapes[3] = new ImageIcon("src/images/rectangle.jpg");
    Shapes[4] = new ImageIcon("src/images/Ellipse.jpg");
    Shapes[5] = new ImageIcon("src/images/heart.jpg");
    Shapes[6] = new ImageIcon("src/images/star.jpg");
    Shapes[7] = new ImageIcon("src/images/triangle.jpg");
}

}

但他们没有用我的图片填充按钮,它们只是显示为空白。有什么建议吗?

import javax.swing.ImageIcon;
import javax.swing.JButton;


public class ShapeButtons extends JButton{
static ImageIcon[] Shapes = new ImageIcon[8];
  static {
    Shapes[0] = new ImageIcon("src/images/Circle.jpg");
        Shapes[1] = new ImageIcon("src/images/Diamond.jpg");
        Shapes[2] = new ImageIcon("src/images/Square.jpg");
        Shapes[3] = new ImageIcon("src/images/rectangle.jpg");
        Shapes[4] = new ImageIcon("src/images/Ellipse.jpg");
        Shapes[5] = new ImageIcon("src/images/heart.jpg");
        Shapes[6] = new ImageIcon("src/images/star.jpg");
        Shapes[7] = new ImageIcon("src/images/triangle.jpg");
        }

public ShapeButtons(int picId){
    super ();//call the jButton cunstructor

     this.setIcon(shapes[picId]);// sets an icon the the Jbutton object.
     }

   public ShapeButtons(){
        super ();//call the jButton cunstructor            
         }

    }

您对第一个构造函数的调用应该如下所示。

 for (int i = 0; i<16; i++){

    buttons[i] = new ShapeButtons(someInteger);//for example
    pan.add(buttons[i]);

}

您对第二个构造函数的调用应如下所示。

 for (int i = 0; i<16; i++){

    buttons[i] = new ShapeButtons();//for example
    pan.add(buttons[i]);
    buttons[i].setIcon(buttons[i].Shapes[someInteger])

}