为jframe窗口设置背景图片

转载:http://blog.csdn.net/jdsjlzx/article/details/16831815
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import java.awt.FlowLayout;  
  2.   
  3. import javax.swing.ImageIcon;  
  4. import javax.swing.JButton;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JLabel;  
  7. import javax.swing.JPanel;  
  8.   
  9. public class JFrameBackground {  
  10.   
  11.  private JFrame frame = new JFrame("背景图片测试");  
  12.   
  13.  private JPanel imagePanel;  
  14.   
  15.  private ImageIcon background;  
  16.   
  17.  public static void main(String[] args) {  
  18.   new JFrameBackground();  
  19.  }  
  20.   
  21.  public JFrameBackground() {  
  22.   background = new ImageIcon("003.jpg");// 背景图片  
  23.   JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面  
  24.   // 把标签的大小位置设置为图片刚好填充整个面板  
  25.   label.setBounds(0, 0, background.getIconWidth(),  
  26.     background.getIconHeight());  
  27.   // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明  
  28.   imagePanel = (JPanel) frame.getContentPane();  
  29.   imagePanel.setOpaque(false);  
  30.   // 内容窗格默认的布局管理器为BorderLayout  
  31.   imagePanel.setLayout(new FlowLayout());  
  32.   imagePanel.add(new JButton("测试按钮"));  
  33.   
  34.   frame.getLayeredPane().setLayout(null);  
  35.   // 把背景图片添加到分层窗格的最底层作为背景  
  36.   frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));  
  37.   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  38.   frame.setSize(background.getIconWidth(), background.getIconHeight());  
  39.   frame.setResizable(false);  
  40.   frame.setVisible(true);  
  41.  }  
  42. }  

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public static void main (String[] args) {         
  2. JFrame frame=new JFrame("背景图设置");          
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
  4. ImageIcon img = new ImageIcon("bg\\1.gif");//这是背景图片     
  5. JLabel imgLabel = new JLabel(img);//将背景图放在标签里。        
  6. frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。      
  7. imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());//设置背景标签的位置     
  8. Container cp=frame.getContentPane();     
  9. cp.setLayout(new BorderLayout());      
  10. JButton but=new JButton("anniu");//创建按钮    
  11. cp.add(but,"North");//将按钮添加入窗口的内容面板        
  12. ((JPanel)cp).setOpaque(false); //注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示出来。        
  13. frame.setSize(500,300);   frame.setVisible(true);                      
  14. }       

 

 

 

通过为jframe设置背景图片,让我明白了以下的知识要点:  
(1)jframe窗口的组成部分,最底层是jrootpane面板。(这一点恐怕很多初学者都没有注意吧!)  
(2)jframe的组成如下:  jrootpane中包含glasspane和layeredpane两个面板。而layeredpane面板包含contentpane和jmenubar。(没想到吧contentpane是放在contentpane中的?)                            
(3)在jframe上添加组件,往往是添加在contentpane中。。但是在contentpane的下面还有两层面板,那就是layeredpane和jrootpane。  

(4)任何一本关于java的书中都会介绍contentpane,却很少提到layeredpane和jrootpane,因此使得很多的初学者产生:jframe中只要一个contentpane的错误认识。 通过解决背景设置的问题,让我对jframe中容器的“层”结构,

 

更多参考:

 

 

  从网上搜索了有关设置背景图片的文章,但是因为我每次设计窗口程序的时候,喜欢利用“Degsin”按钮,将所有的窗口进行布局后,在进行相关源代码的填写,因此,网页提供的答案是直接在主函数中编写,而我选择了在构造函数中编写,故有一定的不同。相关代码如下:

主函数:

public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     HAPPY frame = new HAPPY();
     //frame.setVisible(true);      这行代码,可加可不加,并不会影响最终结果,但是在构造函数中一定要添加;
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

构造函数(关键代码):

JFrame frame=new JFrame("\设\置\背\景\图\片 ");   
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
     ImageIcon img = new ImageIcon("src/images/1.jpg");//这是背景图片   
     JLabel imgLabel = new JLabel(img);//将背景图放在标签里。   
   
     frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签添加到jfram的     LayeredPane面板里。   
     imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());//设置背景标签的位置   
     Container cp=frame.getContentPane();   
     cp.setLayout(null);      //这里选择绝对布局管理器,对于边界布局管理器,放入控件后,无法显示背景图片;因为将整个面板都填充满了;
     ((JPanel)cp).setOpaque(false); //这样就能显示出背景图片出来了。

剩下的就是在面板中添加相关的控件,添加语句可以用:

(1)frame.getContentPane().add(panel);        (2)cp.add(panel)            

效果一样;


另一种方法则是直接为面板设置背景图片,源代码如下:

contentPane = new JPanel(){
   private static final long serialVersionUID=-1588458291133087637L;
   public void paint(Graphics g){
    ImageIcon icon=new ImageIcon("src/images/5.jpg");
    Image image=icon.getImage();
    g.drawImage(image, 0, 0, null);
   }
  };

但在实验中发现,显示效果不如前一种方法,不知为何,面板上设置的标签文字显示不出来,所以,后一种方法虽然更简便,但似乎前一种方法效果更好!

第三种方法:

contentPane.setOpaque(false);

JLabel backgroundLabel = new JLabel("");
        ImageIcon background = new ImageIcon(BalloonMove.class.getResource("/images/background.jpg"));
        backgroundLabel.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
        backgroundLabel.setIcon(background);
        getLayeredPane().add(backgroundLabel, new Integer(Integer.MIN_VALUE));

窗口中的标签,可以直接添加到contentPane面板中,很显然,最后一种方法显示效果很好,且代码简便。

posted @ 2016-03-28 09:24  坚哥威武  阅读(7293)  评论(0编辑  收藏  举报