我创建了一个扩展JPanel的图片类(PicPanel),我创建了一个PicPanel []图片;我填写了新的PicPanel类。 使用循环我将这些类添加到帧中,但只显示最后一张图片。
private void picStore(String[] filePaths)
{
for (int i = 0; i < filePaths.length; i++)
{
pics[i] = new PicPanel(filePaths[i], randomizeCords());
this.add(pics[i]);
}
}
这是我使用的paintComponent:
@Override
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
Image image = pic.getImage();
double scaleFactor = (double)image.getWidth(null)/(double)image.getHeight(null);
g.drawImage(pic.getImage(), loc.x, loc.y, (int)(100 * scaleFactor), 100, null);
}