這里我就放上改寫的代碼吧,不做多的解釋,推薦一個好的博文
https://blog.csdn.net/jdsjlzx/article/details/16831815
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 }