简体   繁体   中英

Is it possible to pass a String variable as argument to new ImageIcon()?

Im adding a jlabel with an image to a jpanel. If I hard code the image url then it works fine but I want an array of URLs so I wanted to know if the command jl.setIcon(new ImageIcon(????)) could be passed a String variable?

private JPanel right;
private JLabel jl= new JLabel();

right = new JPanel();
right.setLayout(new GridLayout(1,1,10,10));
this.add(right, BorderLayout.CENTER);

jl.setIcon(new ImageIcon(ships[0]));
right.add(jl);
validate();

According the the docs it should accept a path to the file, assuming ships[0] is a String.

 public ImageIcon(String filename)

http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.lang.String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM