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.FlowLayout;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
public class Jframebackground {private JFrame frame = new JFrame ("background picture test");
Private JPanel Imagepanel;
private ImageIcon background;
public static void Main (string[] args) {new Jframebackground (); Public Jframebackground () {background = new ImageIcon ("003.jpg");//Background picture JLabel label = new JLabel (background);// Show the background picture in a tag///Set the size position of the label to the picture just fill the entire panel label.setbounds (0, 0, background.geticonwidth (), Background.geticonheight
());
The content pane is converted to JPanel, otherwise it cannot be setopaque () to make the content pane transparent Imagepanel = (JPanel) frame.getcontentpane ();
Imagepanel.setopaque (FALSE);
The default layout manager for the Content pane is BorderLayout imagepanel.setlayout (new FlowLayout ());
Imagepanel.add (New JButton ("Test button"));
Frame.getlayeredpane (). setlayout (NULL); Add the background picture to the bottom of the hierarchy pane as Background Frame.getlayeredpane (). Add (Label, New Integer (Integer.min_value));
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.setsize (Background.geticonwidth (), Background.geticonheight ());
Frame.setresizable (FALSE);
Frame.setvisible (TRUE); }
}
public static void main (String[] args) {
Jframe frame=new jframe ("Background map settings"); Frame.setdefaultcloseoperation (jframe.exit_on_close); ImageIcon Img = new imageicon ("Bg\\1.gif");//This is the background picture Jlabel imglabel = new jlabel (IMG);//Put the background picture 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 button to the content Panel of the window ((JPanel) CP). Setopaque (false);nbsp;//note here, set the content panel 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 the JFrame is as follows: 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 background setting problem, let me jframe the "layer" structure of the container in the
More references:
I've been searching the Internet for articles about setting up background pictures, but because every time I design a window program, like to use the "Degsin" button, all the Windows layout, in the relevant source code filled out, so the answer to the Web page is written directly in the main function, and I chose to write in the constructor, So there is a certain difference. The relevant code is as follows:
Main function:
public static void Main (string[] args) {
Eventqueue.invokelater (New Runnable () {
public void Run () {
try {
HAPPY frame = new HAPPY ();
Frame.setvisible (TRUE); This line of code, can be added without adding, and will not affect the final result, but in the constructor must be added;
catch (Exception e) {
E.printstacktrace ();
}
}
});
}
Constructor (critical code):
JFrame frame=new JFrame ("\ set \ back \ king \ Picture \ Slice")
Frame.setdefaultcloseoperation (jframe.exit_on_close);
ImageIcon img = new ImageIcon ("src/images/1.jpg");//This is the background picture
JLabel imglabel = new JLabel (IMG);//Put the background picture in the label.   &NBSP
Frame.getlayeredpane (). Add (Imglabel, new Integer (Integer.min_value);/Note this is the key, add the background tag to the Jfram layeredpane panel.   &NBSP
imglabel.setbounds (0,0,img.geticonwidth (), Img.geticonheight ()); /Set the position of the background label
Container cp=frame.getcontentpane ();
cp.setlayout (null); //here, select the absolute layout manager, for the boundary layout manager, The background picture cannot be displayed when the control is placed, because the entire panel is filled,
(JPanel) CP). Setopaque (false);//This will show the background picture.
The rest is to add the relevant controls to the Panel, and add the statements to:
(1) Frame.getcontentpane (). Add (Panel); (2) Cp.add (panel)
Effect of the same;
Another method is to set the background picture directly for the panel, the source code is as follows:
ContentPane = new JPanel () {
Private static final long serialversionuid=-1588458291133087637l;
public void Paint (Graphics g) {
ImageIcon icon=new ImageIcon ("src/images/5.jpg");
Image Image=icon.getimage ();
G.drawimage (image, 0, 0, NULL);
}
};
But in the experiment found that the display is not as good as the previous method, I do not know why, the panel set on the label text does not come out, so the latter method although more convenient, but it seems that the former method of better results.
The third method:
Contentpane.setopaque (FALSE);
JLabel Backgroundlabel = new JLabel ("");
ImageIcon background = new ImageIcon (BalloonMove.class.getResource ("/images/background.jpg"));
Backgroundlabel.setbounds (0, 0, background.geticonwidth (), Background.geticonheight ());
Backgroundlabel.seticon (background);
Getlayeredpane (). Add (Backgroundlabel, New Integer (Integer.min_value));
The label in the window can be added directly to the ContentPane panel, and it is obvious that the last method will work well and the code is simple.