The first method is to load the picture through JLabel.
Create a ImageIcon object to get the background picture.
ImageIcon img = new ImageIcon ("Image/img1.png");
Creates a JLabel object that loads the image img into the JLabel object.
JLabel JL_BG = new JLabel (IMG); Background
Then, set the location and size of the JL_BG.
This.jl_bg.setBounds (0, 0, 600, 400); Set the position and size, first setlayout (null).
Place the JLabel object JL_BG into the layered panel.
This.getlayeredpane (). Add (JL_BG, New Integer (Integer.min_value));((JPanel) This.getcontentpane (). Setopaque (False ); Set Transparent
As in the code above, Getlayeredpane () Gets a layered panel that places the background image at the bottom and can be used as a background image. We know that there is also a ContentPane container in the JFrame, so the bottom of the placement is obscured by contentpane, so you need to add a second line of code to make this transparent. Note that it needs to be cast.
The second method is to draw the background picture through paint.
This method is also a more common method, first you need to create a BufferedImage object and initialize it to null.
BufferedImage BG = null;
The background image is then loaded.
try {bg = imageio.read (New File ("Image/img1.png"));} catch (Exception e) {e.printstacktrace ();}
Finally, after overriding the Paint method, you can draw this background picture by using the DrawImage method.
public void Paint (Graphics g) {g.drawimage (bg, 0, 0, NULL);
It is best to use double buffering, otherwise there may be a splash screen god horse problem. About double buffering, written in front of it.
The end~
Several methods of setting background picture in Java