如何在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);
答案 0 :(得分:3)
你不应该用引号将数组调用。
相反,您应该尝试以下方法:
ImageIcon icon = new ImageIcon(picList1.get(0));
答案 1 :(得分:1)
问题出在
行ImageIcon icon = new ImageIcon("picList1.get(0)");
它将字符串解释为文件名。您只需要取消引用picList1.get(0)
位。