你们好,现在有一张格式为.jpg的图片,我想把它设为背景,请问如何实现,谢谢

解决方案 »

  1.   

    这需要自己 重载 protected void paintComponent(Graphics g) 函数
      

  2.   

    我实现了,给你个例子
    import javax.swing.*;
    import java.awt.*;public class DrawPanel extends JPanel
    {
        private Dimension size = new Dimension(20,10);
        Image img;
        int imgWidth,imgHeight;
        
        public void setImage(Image i,int width,int height){
          this.img = i;
          this.imgWidth = width;
          this.imgHeight = height;
          repaint();
        }
        
        public DrawPanel(Dimension size)
        {
            super();
            this.size = size;
        }
        
        public DrawPanel(int width,int height)
        {        
            this(new Dimension(width,height));
        }
        
        public Dimension getPreferredSize()
        {
            return size;
        }
     
        public void paintComponent(Graphics g)
        {
           if(img!=null)
             g.drawImage(img,0,0,imgWidth,imgHeight,this);   
          
        }
    };