You need to add images or background images to the container in Java swing.
Provides two simple solutions, one using jpanel and the other using jlabel.
1. jpanel (source code)
Package Oo; import Java. AWT. graphics; import Java. AWT. image; import Java. io. file; import javax. swing. imageicon; import javax. swing. jframe; import javax. swing. jpanel; public class drawing {jframe = new jframe (); public static jpanel gimage = NULL; public drawing () {initframe () ;}// initialization window public void initframe () {// use jpanel to add the background image gimage = new jpanel () {protected void paintcomponent (Graphics g) {imageicon icon = new imageicon ("image \ benbenla.jpg "); image IMG = icon. getimage (); G. drawimage (IMG, 0, 0, icon. geticonwidth (), icon. geticonheight (), icon. getimageobserver (); jframe. setsize (icon. geticonwidth (), icon. geticonheight () ;}}; jframe. settitle ("test background image"); jframe. add (gimage); jframe. pack (); jframe. setvisible (true); jframe. setdefaclocloseoperation (jframe. exit_on_close);} public static void main (string [] ARGs) {New Drawing ();}}
2. jlabel source code
Package swing. draw; import Java. AWT. image; import javax. swing. imageicon; import javax. swing. jframe; import javax. swing. jlabel;/** use jlabel to construct images */public class drawing2 {jlabel jlpic = new jlabel (); jframe = new jframe (); Public drawing2 () {init1frame ();} public void init1frame () {imageicon = new imageicon ("image \ benla.jpg"); icon. setimage (icon. getimage (). getscaledinstance (icon. geticonwidth (), icon. geticonheight (), image. scale_default); system. out. println (icon. geticonheight () + "" + icon. geticonwidth (); jlpic. setbounds (0, 0, 1366,768); jlpic. sethorizontalalignment (0); jlpic. seticon (icon); jframe. setsize (1366,768); jframe. add (jlpic); jframe. pack (); jframe. setdefaclocloseoperation (jframe. exit_on_close); jframe. setvisible (true);} public static void main (string ARGs []) {New drawing2 ();}}