How to add a background image in Java and add a background image in Java

Source: Internet
Author: User

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 );}

}

 

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.