3

好的,所以我从数据库中获取单个图像没有问题。我用它来把它作为一个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);

但是,我需要对多个图像执行此操作,并将每个图像分配JLabelJPanel. 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)) 的非法开始;

4

1 回答 1

7

这条线没有意义:

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

因为您似乎试图同时声明和使用数组,这建议您应该查看有关使用数组的基本 Java 教程,因为该知识库很重要,在尝试数据库编程或Swing GUI 编程。

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

// !!! 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

尽管如果您所做的只是将 JPanel 添加到 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++;
  }
于 2012-02-19T16:07:10.477 回答