閱讀714 返回首頁    go 微軟 go Office


為jframe窗口設置背景圖片

終於成功實現了如何為jframe窗口設置背景圖片了。下麵是示例,請初學swring的朋友們參考學習!

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JFrameBackground {

 private JFrame frame = new JFrame("背景圖片測試");

 private JPanel imagePanel;

 private ImageIcon background;

 public static void main(String[] args) {
  new JFrameBackground();
 }

 public JFrameBackground() {
  background = new ImageIcon("003.jpg");// 背景圖片
  JLabel label = new JLabel(background);// 把背景圖片顯示在一個標簽裏麵
  // 把標簽的大小位置設置為圖片剛好填充整個麵板
  label.setBounds(0, 0, background.getIconWidth(),
    background.getIconHeight());
  // 把內容窗格轉化為JPanel,否則不能用方法setOpaque()來使內容窗格透明
  imagePanel = (JPanel) frame.getContentPane();
  imagePanel.setOpaque(false);
  // 內容窗格默認的布局管理器為BorderLayout
  imagePanel.setLayout(new FlowLayout());
  imagePanel.add(new JButton("測試按鈕"));

  frame.getLayeredPane().setLayout(null);
  // 把背景圖片添加到分層窗格的最底層作為背景
  frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(background.getIconWidth(), background.getIconHeight());
  frame.setResizable(false);
  frame.setVisible(true);
 }
}

public static void main (String[] args) {       
JFrame frame=new JFrame("背景圖設置");        
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
ImageIcon img = new ImageIcon("bg\\1.gif");//這是背景圖片   
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(new BorderLayout());    
JButton but=new JButton("anniu");//創建按鈕  
cp.add(but,"North");//將按鈕添加入窗口的內容麵板      
((JPanel)cp).setOpaque(false); //注意這裏,將內容麵板設為透明。這樣LayeredPane麵板中的背景才能顯示出來。      
frame.setSize(500,300);   frame.setVisible(true);                    
}     


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



最後更新:2017-04-03 14:54:23

  上一篇:go Java類集--LinkedList類
  下一篇:go Java類集--認識類集、Collection接口