How to add a background image in Java and add a background image in Java
First, import the related packages:
Import java. awt. BorderLayout;
Import java. awt. Container;
Import javax. swing. ImageIcon;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JPanel;
You can also use:
Import java. awt .*;
Import javax. swing .*;
Create a Java form object and set it to visible
Eg: JFram frame = JFrame (); // create a form
Frame. setVisible (true); // sets the visible form, which is invisible by default.
Finally, write the following code:
/*
* The following seven lines Add a background image to the form:
*/
ImageIcon img = new ImageIcon ("E: \ 1" + ". gif"); // This is the background image.
JLabel imgLabel = new JLabel (img); // place the background image in the label.
Frame. getLayeredPane (). add (imgLabel, new Integer (Integer. MIN_VALUE); // note that this is the key. add the background tag to the LayeredPane panel of jfram.
ImgLabel. setBounds (, img. getIconWidth (), img. getIconHeight (); // you can specify the position of the background tag.
Container cp = frame. getContentPane ();
Cp. setLayout (new BorderLayout ());
(JPanel) cp). setOpaque (false); // note that the content panel is transparent. In this way, the background in the LayeredPane Panel can be displayed.
Write a try !!!
The complete code is as follows:
Chapter 8 of package;
Import java. awt. BorderLayout;
Import java. awt. Container;
Import javax. swing. ImageIcon;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JPanel;
Public class MyFirstFrame extends JFrame {
/**
* Author: Listener Valley ~
* Release 1.0
*/Private static final long serialVersionUID = 1L;
Public static void main (String [] args ){
// TODO Auto-generated method stub
MyFirstFrame frame = new MyFirstFrame ();
Frame. setVisible (true); // sets the visible form, which is invisible by default.
/*
* The following seven lines Add a background image to the form:
*/
ImageIcon img = new ImageIcon ("E: \ 1.gif"); // This is the background image.
JLabel imgLabel = new JLabel (img); // place the background image in the label.
Frame. getLayeredPane (). add (imgLabel, new Integer (Integer. MIN_VALUE); // note that this is the key. add the background tag to the LayeredPane panel of jfram. ImgLabel. setBounds (, img. getIconWidth (), img. getIconHeight (); // you can specify the position of the background tag.
Container cp = frame. getContentPane ();
Cp. setLayout (new BorderLayout ());
(JPanel) cp). setOpaque (false); // note that the content panel is transparent. In this way, the background in the LayeredPane Panel can be displayed.
}
Public MyFirstFrame (){
Super (); // inherit the parent class constructor setTitle ("form created using the JFrame class ");
SetBounds (100,100,315,320); // you can specify the position and size of the displayed form.
System. out. println (getForeground ());
GetContentPane (). setLayout (null); // set to no layout manager
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );}
}