Data from: http://blog.csdn.net/qq_32006373/article/details/49659129
http://yuncode.net/code/c_5196327caac3917
In doing a Java big job, do a login interface after feeling too monotonous, so want to say add a background. First directly to get a imageicon, and then changed to image, and then directly on the web to rewrite the paint () method to set the background, success, but found that component are covered out. So began Baidu about JFrame and other containers of the level of problems.
Visually, the structure of the jframe is this:
Can be seen,
The bottom of Frame is RootPane,
And then the Layeredpane.
And the top is contentpane.
The top floor is glasspane.
The topmost glasspane is transparent by default, and this ContentPane is the method that we usually use to add controls, and now that ContentPane
ContentPane default is opaque, the complete way to add controls is:
Frame.getContentPane.add (...);
Now abbreviated into Frame.add (...);
It can also be said that ContentPane is a jpanel placed on the Layeredpane.
So let's look at the level of jframe from a model point of view:
Now let's talk about a background:
The first step is to understand the hierarchy in the framework jframe.
The hierarchical distribution and relative relationships in JFrame are:
The bottom is: jrootpane; the second layer is: Jlayerpane, which contains a layer of ContentPane (the default is opaque), is also what we often say the content panel. So the most common control we drag and drop is on the ContentPane layer. The top is: GlassPane (which is transparent by default), then here's a method: principle: We put the picture on the second layer: Jlayerpane container (using JLabel), and then let the top
The contentpane is transparent so that the setting of the background is realized. Look at the code:
JPanel jpanelofframe= (JPanel) This. Getcontentpane ();//get Contentpane,jpanel There's a way to set up transparency//set the part of the background pictureString imageUrl = "Src/image/f0f600978623446f8ae2d189d3ac0fdf.jpg";//Background Map PathImageIcon icon =NewImageIcon (IMAGEURL); JLabel Labelbackground=NewJLabel (icon); Labelbackground.setbounds (0, 0, icon.geticonwidth (), Icon.geticonheight ());//set this label so that the picture is just full of labels This. Getlayeredpane (). Add (Labelbackground,NewInteger (Integer.min_value));//This is JFrameJpanelofframe.setopaque (false);//set ContentPane to Transparent
About the hierarchy of top-level containers such as jframe in swing and how to set the background