How to add background images to Oracle Java programs

Source: Internet
Author: User

The following articles mainly describe how to add beautiful background images to the actual Oracle Java-related applications. The actual application code that can be executed after sorting is as follows: I hope this article will help you in its practical application.

 
 
  1. Import java. awt .*;
  2. Import javax. swing .*;
  3. Public class TestBackgroundColor extends JFrame
  4. {
  5. Public static void main (String [] args)
  6. {
  7. TODO Auto-generated method stub
  8. TestBackgroundColor tbc = new TestBackgroundColor ();
  9. Tbc. setVisible (true );
  10. }
  11. Private JPanel imagePanel;
  12. Private ImageIcon background;
  13. Public TestBackgroundColor ()
  14. {
  15. Background = new ImageIcon ("gradient background 14.png ");

Background Image

 
 
  1. JLabel label = new JLabel(background); 

Display the background image in a tag

Set the label size and position to the image to fill the entire panel.

 
 
  1. label.setBounds(0,0,background.getIconWidth(),background.getIconHeight()); 

Convert the content pane to JPanel. Otherwise, you cannot use the setOpaque () method to make the content pane transparent.

 
 
  1. imagePanel = (JPanel)this.getContentPane();  
  2. imagePanel.setOpaque(false);  

The default layout manager of the content pane is BorderLayout.

 
 
  1. ImagePanel. setLayout (new FlowLayout ());
  2. ImagePanel. add (new JButton ("test button "));
  3. This. getLayeredPane (). setLayout (null );

Add the background image to the bottom layer of the layered pane as the background

 
 
  1. this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));  
  2. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  3. this.setSize(background.getIconWidth(),background.getIconHeight());  
  4. this.setVisible(true);  
  5. }  
  6. }  

The above content is an introduction to adding beautiful background images to the Oracle Java program. I hope you will have some gains.

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.