自己写的BackgroundPanel(面板中添加背景图片的方法)

以下是自己写的BackgroundPanel.java文件的内容

package function;

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


import javax.swing.JPanel;




public  class BackgroundPanel extends JPanel
{
private static final long serialVersionUID=-7411632419492480055L;
private Image image;


public BackgroundPanel(Image image) {  
this.image=image;
this.setOpaque(false);

}


@Override
protected void paintComponent(Graphics g) 
 {
super.paintComponent(g);
if (image != null) {
int width=getWidth();
int height=getHeight();
g.drawImage(image, 0,0,width,height,this);
}
}

}


然后是使用它的方式:

static JPanel beijing;

public static void main(String[] args) {

beijing = new BackgroundPanel(new ImageIcon("image\\denglu-image\\c.gif").getImage());

你可能感兴趣的:(自己写的BackgroundPanel(面板中添加背景图片的方法))