java学习日记----------------为窗口添加背景图

   最近一直忙着做java课程设计,我就纳闷了,这么重要的课学校为什么只安排八周时间。。。。。明明重点都没学了,

    哎,不发恼骚了,最近课程设计收获挺多的,这里记录一个小知识点,方便以后查阅。

import javax.swing.*;
/*
 *  JFrame的三个层次从低到高:RootPane、LayeredPane、ContentPane
 *  RootPane默认是可视的,只要上面两层透明或不可见
 *  LayeredPane默认是透明的(Opaque)
 *  ContentPane默认可视,一般组件都在这一层
 */
public class AddPicture extends JFrame {
    private  ImageIcon imageicon=new ImageIcon("D:/JAVA/Eclipse/书1.jpg");//图片路径
    private JLabel label_background=new JLabel(imageicon);//
    public  AddPicture(){
        super("添加背景图片");
        this.setBounds(300,240, imageicon.getIconWidth(), imageicon.getIconHeight());//设置窗口大小为图片大小
        label_background.setBounds(0, 0, this.getWidth(), this.getHeight());
        this.getLayeredPane().add(label_background,new Integer(Integer.MIN_VALUE));//将有图片的标签添加到JFrame的第二层
        ((JPanel)this.getContentPane()).setOpaque(false);//将contentpane层设置不可见,注意,此处需要转变为面板类
        
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new AddPicture();
    }
}

原文地址:https://www.cnblogs.com/he-shao/p/4947535.html