java-How to set a background picture for the frame window __java

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.