Finally successfully implemented how to set the background picture for the JFrame window. Here is an example, please learn from Swring's friends.
Import java.awt.*; Import javax.swing.*; Import Java.awt.Container; public class Framebg {public Framebg () {} public static void Main (string[] args) {JFrame frame=new JFrame ("Background map Settings"); fr Ame.setdefaultcloseoperation (Jframe.exit_on_close); ImageIcon img = new ImageIcon ("Bg//1.gif");//This is a background image JLabel Imglabel = new JLabel (IMG);//The background image is placed in the label. Frame.getlayeredpane (). Add (Imglabel, New Integer (integer.min_value);/Note this is the key, add the background tag to the Jfram layeredpane panel. Imglabel.setbounds (0,0,img.geticonwidth (), Img.geticonheight ());//Set the position of the background label Container Cp=frame.getcontentpane (); Cp.setlayout (New BorderLayout ()); JButton but=new JButton ("Anniu");//Create button Cp.add (but, "North");//Add the button to the window's content panel ((JPanel) CP). Setopaque (false); Note that the content panel is set to transparent. This allows the background in the Layeredpane panel to be displayed. Frame.setsize (500,300); Frame.setvisible (TRUE); } }
By setting a background picture for JFrame, I understand the following points of knowledge:
(1) JFrame window components, the bottom is the JRootPane panel. I'm afraid many beginners are not paying attention to it. )
(2) The composition of JFrame is as follows:
The JRootPane contains GlassPane and layeredpane two panels. The Layeredpane panel contains ContentPane and JMenuBar. (Unexpectedly, ContentPane is placed in the ContentPane.) )
(3) Adding components to the jframe is often added to the contentpane. But underneath the contentpane there are two layers of panels, which are layeredpane and JRootPane.
(4) Any book on Java will introduce ContentPane, but rarely mentions Layeredpane and jrootpane, so many beginners are produced: JFrame as long as a contentpane of mistaken understanding.
By solving the problem of background setting, let me have a deeper understanding of the "layer" structure of the container in the JFrame.
The above knowledge point, hope the beginner swing's friends attention.
ok! just share. Let's learn and progress together.