Java Swing 之设置窗体背景图片

时间:2014-05-05 13:19:17   收藏:0   阅读:350
/**
 * 设置窗体背景图片
 * @author gao
 */
package com.gao;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BackgroundDemo extends JFrame{
	private Image image;
	private static final long serialVersionUID=-1588458291133087637L;
    public BackgroundDemo(){
    	this.setTitle("设置窗体的背景颜色");
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	this.setBounds(0 , 0, 400,300 );
    	
    	JPanel contentPane=new JPanel(){
    		@Override
    		public void paint(Graphics g) {
    			ImageIcon icon=new ImageIcon("2.jpg");
    	 image=icon.getImage();
    	 g.drawImage(image, 0,0,null);
    			// TODO Auto-generated method stub
    			
    		}
    	};	 
    	this.add(contentPane);
    	this.setVisible(true);
    }
    
    
    public static void main(String[]args){
    	BackgroundDemo example=new BackgroundDemo();

    }
}
bubuko.com,布布扣

Java Swing 之设置窗体背景图片,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!