設置JFrame背景圖片

這裏我就放上改寫的代碼吧,不作多的解釋,推薦一個好的博文this

http://www.javashuo.com/article/p-womajldp-gs.htmlspa

 1 public void ini_background() {
 2     // 背景圖片
 3     ImageIcon background = new ImageIcon(
 4                 this.getClass().getResource("/img/登陸背景.png"));
 5     // 把背景圖片顯示在一個標籤裏面
 6     JLabel label = new JLabel(background);
 7     // 設置標籤大小
 8     label.setBounds(0, 0, 440, 335);
 9     // 把內容窗格轉化爲JPanel,不然不能用方法setOpaque()來使內容窗格透明
10     JPanel imagePanel = (JPanel) this.getContentPane();
11     imagePanel.setOpaque(false);
12     this.getLayeredPane().setLayout(null);
13     // 把背景圖片添加到分層窗格的最底層做爲背景
14     this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
15 }