首页 文章

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

提问于
浏览
3

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

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 分配给 CardLayout 中的新 JPanel . 我知道我需要某种循环,寻找最佳方法的建议!

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 回答

  • 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++;
      }
    

相关问题