Oracle Java程序添加背景图片的操作方案


  本文标签:Oracle Java

  以下的文章主要是对Oracle Java的相关的实际应用程序添加漂亮的背景图片的实际操作的描述,其在整理之后可执行的相关实际应用代码如下:希望本文章会给你在其实际应用方面带来一些帮助在此方面  。

  

  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("渐变背景14.png");  

  

  背景图片

  

  1. JLabel label = new JLabel(background); 

  把背景图片显示在一个标签里面

  把标签的大小位置设置为图片刚好填充整个面板

  

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

  把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明

  

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

  

  内容窗格默认的布局管理器为BorderLayout

  

  1. imagePanel.setLayout(new FlowLayout());  
  2. imagePanel.add(new JButton("测试按钮"));  
  3. this.getLayeredPane().setLayout(null);  

  

  把背景图片添加到分层窗格的最底层作为背景

  

  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. }  

  

  以上的相关内容就是对为Oracle Java程序添加漂亮背景图片的介绍,望你能有所收获  。