為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面板中,很顯然,最后一種方法顯示效果很好,且代碼簡便。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM