利用JPanel和JLabel设置背景图片

     //创建面板1,放置背景图片1
        JPanel jPanelTop=new JPanel();
        jPanelTop.setBounds(0,-5,100,100);  //x=0,y=-5用来设置面板距离窗体左上角的距离(单位是像素)
        jPanelTop.setOpaque(true);
        JLabel jLabel=new JLabel();  //创建JLabel标签,用来存放背景图片
        jLabel.setBounds(0,0,100,100);
        Icon icon=new ImageIcon("doc/ChinaJiLiang.png"); //根据所给图片,生成指定大小的图标
        jLabel.setIcon(icon);    //设置标签要显示的图标
        jPanelTop.add(jLabel);   //将标签添加到面板中

猜你喜欢

转载自www.cnblogs.com/luckyplj/p/10265240.html