首页 > 编程语言 > 详细

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

时间:2015-11-08 16:28:32      阅读:210      评论:0      收藏:0      [点我收藏+]

   最近一直忙着做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();
    }
}

技术分享

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

原文:http://www.cnblogs.com/he-shao/p/4947535.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!