使用Java在数据库中显示多个图像

时间:2012-02-19 14:19:13

标签: java database image swing imageicon

好的,所以我从数据库中获取单个图像没问题。我使用它将其作为ImageIcon上的JLabel返回

while (rs.next()) {
images = rs.getString("path");
System.out.println(images + "\n");
System.out.println("TESTING - READING IMAGE");
BufferedImage img = ImageIO.read(new File(images));
System.out.println("img = " + images);
imagelabel = new JLabel(new ImageIcon(img));
imagelabel.setPreferredSize(new Dimension(200, 200));
imageselect.add(imagelabel);

但是,我需要对多个图片执行此操作,并将每个JLabel分配给JPanel中的新CardLayout。我知道我需要某种循环,寻找最佳方法的建议!

BufferedImage imgA = ImageIO.read(new File("lmkpackage/images/one.jpg"));
            image1 = new JLabel(new ImageIcon(imgA));
            image1.setPreferredSize(new Dimension(200, 200));
            img1 = new JPanel();        
            img1.add(image1);

            loadcard.add(img1,"1");
            cl2.show(loadcard,"1");

            BufferedImage imgB = ImageIO.read(new File("lmkpackage/images/two.jpg"));
            image2 = new JLabel(new ImageIcon(imgB));
            image2.setPreferredSize(new Dimension(200, 200));
            img2 = new JPanel();        
            img2.add(image2);

            loadcard.add(img2, "2");

            BufferedImage imgC = ImageIO.read(new File("lmkpackage/images/three.jpg"));
            image3 = new JLabel(new ImageIcon(imgC));
            image3.setPreferredSize(new Dimension(200, 200));
            img3 = new JPanel();        
            img3.add(image3);

            loadcard.add(img3, "3");

            BufferedImage imgD = ImageIO.read(new File("lmkpackage/images/four.jpg"));
            image4 = new JLabel(new ImageIcon(imgD));
            image4.setPreferredSize(new Dimension(200, 200));
            img4 = new JPanel();
            img4.add(image4);

            loadcard.add(img4, "4");

            BufferedImage imgE = ImageIO.read(new File("lmkpackage/images/five.jpg"));

            image5 = new JLabel(new ImageIcon(imgE));
            image5.setPreferredSize(new Dimension(200, 200));
            img5 = new JPanel();        
            img5.add(image5);

            loadcard.add(img5, "5");

按照要求,这是我的尝试:

while (rs.next()) {
                        images = rs.getString("path");
                        System.out.println(images + "\n");
                        System.out.println("TESTING - READING IMAGE");
                        for(i=0; i < 5; i++){                       
                        BufferedImage img[i] = ImageIO.read(new File(images));
                        imglab[i] = new JLabel(new ImageIcon(imgIcon[i]));
                        imgPanel[i]= new JPanel();
                        imgPanel[i].add(imglab[i]);
                        loadcard.add(imgPanel[i], i);                                       
                        }//End For
                    }//EndWhile

我得到的错误是:

letmeknow.java:181:']'期望BufferedImage img [i] = ImageIO.read(new File(images)); letmeknow.java:181:非法启动表达式BufferedImage img [i] = ImageIO.read(new File(images));

1 个答案:

答案 0 :(得分:5)

这条线毫无意义:

BufferedImage img[i] = ImageIO.read(new File(images));

因为您似乎正在尝试同时声明和使用数组,这表明您应该查看有关数组使用的基本Java教程,因为这个知识库很关键,并且在尝试之前应该是众所周知的。数据库编程或Swing GUI编程。

为了解决这个问题,在 while循环之前声明你的数组(或者可能更好 - ArrayList )BufferedImage ,然后在循环中使用它。例如:

// !!! CAVEAT: code not compiled nor tested !!!

// TOTAL_IMAGE_COUNT is a constant that defines the array size
// an ArrayList might be better though
BufferedImage[] myImages = new BufferedImage[TOTAL_IMAGE_COUNT];
int i = 0;
while (rs.next()) {
    String imagePath = rs.getString("path");
    System.out.println(imagePath + "\n");
    System.out.println("TESTING - READING IMAGE");

    myImages[i] = ImageIO.read(new File(imagePath));
    imglab[i] = new JLabel(new ImageIcon(myImages[i]));
    imgPanel[i]= new JPanel();
    imgPanel[i].add(imglab[i]);
    loadcard.add(imgPanel[i], i);     
    i++;                 
}//EndWhile

如果你所做的只是将JPanels添加到CardLayout中,甚至可能都不需要所有这些数组。我将图像文件路径存储在数据库而不是图像本身中似乎有点奇怪。您的图像文件名称似乎微不足道,甚至可能甚至不需要数据库。也许你需要的只是一件非常简单的事情:

  String imageLocation = "lmkpackage/images/";
  String[] imageNames = {"one", "two", "three", "four", "five"};
  String imgExt = ".jpg";

  int count = 1;

  for (String imageName : imageNames) {
     String imagePath = imageLocation + imageName + imgExt;
     BufferedImage img = ImageIO.read(new File(imagePath));
     ImageIcon icon = new ImageIcon(img);
     JLabel label = new JLabel(icon);
     loadcard.add(label, String.valueOf(count));
     count++;
  }