0

如何在 JPanel 中显示存储在 arraylist 中的 jpg 图像?我无法在 JPanel 中显示 jpg 文件。

String[] pictureFile   = {"A.jpg","B.jpg","C.jpg"};
List<String>  picList1 = Arrays.asList(pictureFile);

Collections.shuffle(picList1); 

ImageIcon icon = new ImageIcon("picList1.get(0)");
JLabel label1   = new JLabel();
label1.setIcon(icon);

JPanel panel = newJPanel;
panel.add(label); 
4

2 回答 2

3

你不应该把对数组的调用放在引号中。

相反,您应该尝试以下方法:

ImageIcon icon = new ImageIcon(picList1.get(0));
于 2009-11-01T17:43:41.140 回答
1

问题出在一线

ImageIcon icon = new ImageIcon("picList1.get(0)");

它将字符串解释为文件名。您应该只需要取消引用该picList1.get(0)位。

于 2009-11-01T17:43:02.297 回答