添加JLabel与图像到JList显示所有的图像

这是我的代码。 它不会在框架中显示图像,而是显示一些文字。 任何人都请告诉我,我应该在代码中做出什么样的改变,以便我可以在一个框架中显示图像?

import java.awt.Component; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.DefaultListModel; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JScrollPane; public class ListView { public static void main(String[] args) throws IOException { JFrame frame=new JFrame(); frame.setSize(500,500); JLabel lbl[] = new JLabel[10]; DefaultListModel listModel; ImageIcon[] b = new ImageIcon[10]; //JList lsm=new JList(); listModel = new DefaultListModel(); File folder = new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures"); File[] listOfFiles = folder.listFiles(); JLabel[] lb=new JLabel[15]; for (int i = 0; i < listOfFiles.length; i++) { System.out.println("chek panth"+listOfFiles[i].getName().toString()); // b[i] = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/" + listOfFiles[i].getName().toString())); b[i] = new ImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/" + listOfFiles[i].getName().toString()); lb[i]=new JLabel(b[i]); listModel.add(i, lb[i]); } JList lsm=new JList(listModel); Component add = frame.add(new JScrollPane(lsm)); frame.setVisible(true); } } 

请注意,我不会以这种方式devise代码,但是我希望尽可能使其与原始代码保持一致,同时使其能够在基于Windows的方框上显示图像列表。

列表显示

 import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ListView { public static void main(String[] args) throws IOException { String path = "C:/Documents and Settings/All Users/Documents/" + "My Pictures/Sample Pictures"; JFrame frame=new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); File folder = new File(path); File[] listOfFiles = folder.listFiles(); DefaultListModel listModel = new DefaultListModel(); int count = 0; for (int i = 0; i < listOfFiles.length; i++) { System.out.println("check path"+listOfFiles[i]); String name = listOfFiles[i].toString(); // load only JPEGs if ( name.endsWith("jpg") ) { ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i])); listModel.add(count++, ii); } } JList lsm=new JList(listModel); lsm.setVisibleRowCount(1); frame.add(new JScrollPane(lsm)); frame.pack(); frame.setVisible(true); } } 

您可以使用listcellrenderer在jlist中同时显示图像和文本,如下所示,用于在列表中显示带有图标的标签

  public class myRenderer extends DefaultListCellRenderer { @Override public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus) { //JLabel l = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if(value instanceof JLabel) { this.setText(((JLabel)value).getText()); this.setIcon(((JLabel)value).getIcon()); } return this; } }