Swing在JPanel加背景图片的方法
package com.tntxia.commonswing.panel;import java.awt.*;import javax.swing.JPanel;/** * 有背景图片的Panel类 * @author tntxia * */public class BackgroundPanel extends JPanel {/** * */private static final long serialVersionUID = -6352788025440244338L;private Image image = null;public BackgroundPanel(Image image) {this.image = image;}// 固定背景图片,允许这个JPanel可以在图片上添加其他组件protected void paintComponent(Graphics g) {g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);}}
?
调用的方法:
?
Image image=new ImageIcon("images/bg.gif").getImage();JPanel panel = new BackgroundPanel(image);
?