swing开发JPanel设置背景图
实现代码
1、工具类BackGroundImagePanle类
package centralControl.tool;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
* 带背景图的jpanel
* qxh
* 2019年9月19日16:42:20
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class BackGroundImagePanle extends JPanel{
private ImageIcon icon;
/*public ImageIcon getIcon() {
return icon;
}*/
public void setIcon(ImageIcon icon) {
this.icon = icon;
}
private int x = 0;
private int y = 0;
public BackGroundImagePanle(String url) {
// TODO Auto-generated constructor stub
icon = new ImageIcon(this.getClass().getResource(url));
}
public void paintComponent(Graphics g) {
g.drawImage(icon.getImage(), x, y, getSize().width,getSize().height, this);// 图片会自动缩放
}
}
2、使用
private static String LIST_BACK3 = "/centralControl/img/listback3.png";
BackGroundImagePanle ingPanel= new BackGroundImagePanle(LIST_BACK3);