如何为JAVA窗体程序添加背景图片

public Qua_Main_JFrame() {
        JPanel jpanel = new JPanel();
        this.setContentPane(jpanel);  
        //添加标签组件
        GridLayout gird = new GridLayout(3,0);
        jpanel.setLayout(gird);
        ImageIcon img = new ImageIcon("src/JMXY.JPG");
        JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
        this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
        imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());    
        this.getLayeredPane().setLayout(null);
       
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jpanel.setOpaque(false);
        initComponents();
    }

其中Qua_Main_JFrame为创建的java窗体项目名,图片按路径存放,注意一点,所有的代码都应该放在initComponents();方法之上,这样你添加进窗体中的空间才会显示在图片之上,否则看不见控件。

posted @ 2011-03-10 19:32  白龙龙  阅读(5260)  评论(0编辑  收藏  举报