Java JLayeredPane to implement window background images

Source: Internet
Author: User

Use JLayeredPane to add a background image without affecting the layout of the window components:

,

After adjusting the window size:

 

 

[Java]
Package com. han;
 
Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. awt. Dimension;
Import java. awt. Image;
Import java. awt. Point;
Import java. awt. Rectangle;
Import java. awt. Toolkit;
Import java. awt. event. ComponentAdapter;
Import java. awt. event. ComponentEvent;
 
Import javax. swing. BorderFactory;
Import javax. swing. ImageIcon;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JLayeredPane;
Import javax. swing. JPanel;
 
/** Www.2cto.com
* The JLayeredPane has no layout manager by default, so you have to use
* SetBounds () for each component in it. However, you can still assign a layout
* Manager to the layered pane.
* <P>
* Define programs use intermediate containers (such as panels) and their layout
* Managers to lay out components on the same layer, but use absolute
* Positioning to lay out components on different layers.
* <P>
* We can use frame. pack () or frame. setSize () whatever its layout method is
* (Use a layout manager or not ).
* <P>
* In fact, if we hesitate to use component. setPreferredSize () or
* Component. setSize (), we first see the layout method employed to layout this
* Component. If it has a layout manager, we use component. setPreferredSize ();
* Else (no layout manager), we use component. setBounds ().
* <P>
* Concerning the JLayeredPane, the <I> depth </I> is higher, the component is on
* The top (of course, there are some depth conventions, like DEFAULT_LAYER,
* PALETTE_LAYER,...). Within its depth, the position number is 0, top-most;
*-1, bottom-most; 0, 1, 2, 3,... from top to bottom.
*
* @ Author HAN
*
*/
@ SuppressWarnings ("serial ")
Public class JLayeredPane_1 extends JFrame {
 
Public JLayeredPane_1 (){
// TODO Auto-generated constructor stub
Container container = getContentPane ();
 
Final JLayeredPane layeredPane = new JLayeredPane ();
// LayeredPane. setBorder (BorderFactory. createTitledBorder (null,
// "LayeredPane", TitledBorder. LEADING, TitledBorder. TOP,
// New Font (null, Font. BOLD + Font. ITALIC, 14), Color. RED ));
LayeredPane. setBorder (BorderFactory. createTitledBorder (""<U> <font face = 'sserif' size = '5' color = 'red'> layeredPane" +
"</Font> </u> </I> </B> Dimension dimension = new Dimension (300,310 );
LayeredPane. setPreferredSize (dimension );

Final JPanel panelBg = new JPanel ();
Final Image imageBg = Toolkit. getdefatooltoolkit ()
. GetImage (this. getClass (). getResource ("/images/Lighthouse.jpg "));
ImageIcon imageIcon = new ImageIcon (imageBg. getScaledInstance
(Dimension. width, dimension. height, Image. SCALE_FAST ));
Final JLabel bg = new JLabel (imageIcon );
Final Point origin = new Point (10, 30 );
Final Rectangle rectangle = new Rectangle (origin, dimension );
PanelBg. setBounds (rectangle );
PanelBg. add (bg );

Final JPanel panelContent = new JPanel ();
JButton button = new JButton ("button 1 ");
JButton button2 = new JButton ("button 2 ");
JButton button3 = new JButton ("button 3 ");
PanelContent. setBounds (rectangle );
PanelContent. setOpaque (false); // set to transparent
PanelContent. add (button );
PanelContent. add (button2 );
PanelContent. add (button3 );

LayeredPane. add (panelBg, new Integer (0); // the same to layeredPane. add (panelBg );
LayeredPane. add (panelContent, new Integer (1 ));

Container. add (layeredPane, BorderLayout. CENTER );

AddComponentListener (new ComponentAdapter (){
Public void componentResized (ComponentEvent e ){
// System. out. println (layeredPane. getSize (); // getSize () must be used instead of getPreferredSize ()
// System. out. println (layeredPane. getPreferredSize ());
Dimension dimensionNew = layeredPane. getSize ();
Rectangle rectangleNew = new Rectangle (origin, dimensionNew );
PanelBg. setBounds (rectangleNew );
PanelContent. setBounds (rectangleNew );
Bg. setIcon (new ImageIcon (imageBg. getScaledInstance
(DimensionNew. width, dimensionNew. height, Image. SCALE_FAST )));
}
});
}
 
/**
* @ Param args
* @ Throws Exception
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
JLayeredPane_1 frame = new JLayeredPane_1 ();
Frame. pack ();
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setVisible (true );
}
 
}

Package com. han;

Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. awt. Dimension;
Import java. awt. Image;
Import java. awt. Point;
Import java. awt. Rectangle;
Import java. awt. Toolkit;
Import java. awt. event. ComponentAdapter;
Import java. awt. event. ComponentEvent;

Import javax. swing. BorderFactory;
Import javax. swing. ImageIcon;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JLayeredPane;
Import javax. swing. JPanel;

/**
* The JLayeredPane has no layout manager by default, so you have to use
* SetBounds () for each component in it. However, you can still assign a layout
* Manager to the layered pane.
* <P>
* Define programs use intermediate containers (such as panels) and their layout
* Managers to lay out components on the same layer, but use absolute
* Positioning to lay out components on different layers.
* <P>
* We can use frame. pack () or frame. setSize () whatever its layout method is
* (Use a layout manager or not ).
* <P>
* In fact, if we hesitate to use component. setPreferredSize () or
* Component. setSize (), we first see the layout method employed to layout this
* Component. If it has a layout manager, we use component. setPreferredSize ();
* Else (no layout manager), we use component. setBounds ().
* <P>
* Concerning the JLayeredPane, the <I> depth </I> is higher, the component is on
* The top (of course, there are some depth conventions, like DEFAULT_LAYER,
* PALETTE_LAYER,...). Within its depth, the position number is 0, top-most;
*-1, bottom-most; 0, 1, 2, 3,... from top to bottom.
*
* @ Author HAN
*
*/
@ SuppressWarnings ("serial ")
Public class JLayeredPane_1 extends JFrame {

Public JLayeredPane_1 (){
// TODO Auto-generated constructor stub
Container container = getContentPane ();

Final JLayeredPane layeredPane = new JLayeredPane ();
// LayeredPane. setBorder (BorderFactory. createTitledBorder (null,
// "LayeredPane", TitledBorder. LEADING, TitledBorder. TOP,
// New Font (null, Font. BOLD + Font. ITALIC, 14), Color. RED ));
LayeredPane. setBorder (BorderFactory. createTitledBorder (""<U> <font face = 'sserif' size = '5' color = 'red'> layeredPane" +
"</Font> </u> </I> </B> Dimension dimension = new Dimension (300,310 );
LayeredPane. setPreferredSize (dimension );

Final JPanel panelBg = new JPanel ();
Final Image imageBg = Toolkit. getdefatooltoolkit ()
. GetImage (this. getClass (). getResource ("/images/Lighthouse.jpg "));
ImageIcon imageIcon = new ImageIcon (imageBg. getScaledInstance
(Dimension. width, dimension. height, Image. SCALE_FAST ));
Final JLabel bg = new JLabel (imageIcon );
Final Point origin = new Point (10, 30 );
Final Rectangle rectangle = new Rectangle (origin, dimension );
PanelBg. setBounds (rectangle );
PanelBg. add (bg );

Final JPanel panelContent = new JPanel ();
JButton button = new JButton ("button 1 ");
JButton button2 = new JButton ("button 2 ");
JButton button3 = new JButton ("button 3 ");
PanelContent. setBounds (rectangle );
PanelContent. setOpaque (false); // set to transparent
PanelContent. add (button );
PanelContent. add (button2 );
PanelContent. add (button3 );

LayeredPane. add (panelBg, new Integer (0); // the same to layeredPane. add (panelBg );
LayeredPane. add (panelContent, new Integer (1 ));

Container. add (layeredPane, BorderLayout. CENTER );

AddComponentListener (new ComponentAdapter (){
Public void componentResized (ComponentEvent e ){
// System. out. println (layeredPane. getSize (); // getSize () must be used instead of getPreferredSize ()
// System. out. println (layeredPane. getPreferredSize ());
Dimension dimensionNew = layeredPane. getSize ();
Rectangle rectangleNew = new Rectangle (origin, dimensionNew );
PanelBg. setBounds (rectangleNew );
PanelContent. setBounds (rectangleNew );
Bg. setIcon (new ImageIcon (imageBg. getScaledInstance
(DimensionNew. width, dimensionNew. height, Image. SCALE_FAST )));
}
});
}

/**
* @ Param args
* @ Throws Exception
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
JLayeredPane_1 frame = new JLayeredPane_1 ();
Frame. pack ();
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setVisible (true );
}

}


 

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.