3
這是我的代碼。它不會在框架中顯示圖像,而是顯示一些文字。有人會建議我在代碼中應該做些什麼改變,以便它能讓我在一個框架中顯示圖像?添加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);
}
}
感謝嘗試是有益的一個同時顯示圖像和文字: - )只是:a)你很少(如從來沒有)在ListModel中有組件b)請學習java命名約定,並堅持他們 – kleopatra 2012-03-31 12:29:51
這是正確的,但我的朋友遇到麻煩添加jlabel圖標的名單,所以我有以某種方式找到解決方案 – chirag 2012-03-31 16:52:06