Displaying jpg image on JPanel
How can 开发者_如何学编程I display jpg image which I stored in arraylist in JPanel? Im not able to display the jpg files in the JPanel.
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);
You should not put the call to the array in quotes.
Instead, you should try the following:
ImageIcon icon = new ImageIcon(picList1.get(0));
The problem is in the line
ImageIcon icon = new ImageIcon("picList1.get(0)");
It's interpreting the string as a file name. You should just need to unquote the picList1.get(0)
bit.
精彩评论