提问人:Peddler 提问时间:2/19/2012 最后编辑:SastrijaPeddler 更新时间:2/20/2012 访问量:3547
在 Java 中显示数据库中的多个图像
Displaying multiple images from a Database in Java
问:
好的,所以我从数据库中获取单个图像没有问题。我用它来将其作为 on a 返回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));
答:
7赞
Hovercraft Full Of Eels
2/20/2012
#1
这句话没有意义:
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
尽管如果您所做的只是将 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++;
}
评论
0赞
Peddler
2/20/2012
我在网上读到的任何地方,都建议存储图像的路径,而不是将图像存储为 BLOB 文件。我知道这里看起来很简单,但应用程序不仅仅是这个,所以数据库肯定是必需的。无论如何,你一直很有帮助。谢谢!
0赞
Hovercraft Full Of Eels
2/20/2012
@Peddler:不客气。再说一次,我不是数据库专家,所以对任何特定于数据库的建议都持保留态度!
评论