1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Set Icon for JLabel Example This java example shows how to set image icon for JLabel using setIcon method of Java Swing JLabel class. */ import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JLabel; /* <applet code="SetJLabelIconExample" width=200 height=200> </applet> */ public class SetJLabelIconExample extends JApplet{ public void init(){ /* * To create JLabel use * JLabel (String caption) constructor * of JLabel class. */ JLabel label1 = new JLabel("JLabel Set Icon Example."); //add label to applet add(label1); /* * To set image icon for JLabel use, * void setLabel(Icon icon) * method of JLabel class. */ label1.setIcon(new ImageIcon("images/copy.gif")); } } |
🙂 thanks
This is wrong. (Don’t work)
The correct using of the setIcon:
label1.setIcon(new ImageIcon(getClass().getResource(“images/copy.gif”)));