Package com. beyole. util; import java. awt. color; import java. io. file; import javax. swing. icon; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JLabel; public class test4 {public static void main (String [] args) {JFrame frame = new JFrame ("Crystal"); String path = "f:" + File. separator + "1.jpg"; Icon icon = new ImageIcon (path); JLabel label = null; label = new JLabel (icon, JLabel. CENTER); label. setForeground (Color. RED); label. setBackground (Color. YELLOW); frame. add (label); frame. setSize (300,160); frame. setBackground (Color. white); frame. setLocation (300,200); frame. setVisible (true );}}
This instance does not use the input stream to adjust the image, because I think it is too troublesome. It is estimated that it will not be used in future projects. Of course, it is just my personal opinion.
The code for calling the image is only Icon icon = new ImageIcon (path); then you can add the icon to the JLabel constructor.
Program: