如何在JPanel上插入背景图片.

解决方案 »

  1.   

    public class ImagePanel extends JPanel {
        //
        private ImageIcon image = null;
        //
        public ImagePanel(ImageIcon image) {
            this.image = image;
        }    //
        protected void paintComponent(Graphics g) {
            setOpaque(true);
            super.paintComponent(g);        Dimension d = getSize();
            for (int x = 0; x < d.width; x += image.getIconWidth()) {
                for (int y = 0; y < d.height; y += image.getIconHeight()) {
                    g.drawImage(image.getImage(), x, y, null, null);
                }
            }
        }}
      

  2.   

    楼上的 人家问得是插入图片,你瞎贴上个图片平铺的代码 也不说明一下...
    主要就是一个g.drawImage();方法。其他的都没用。
    具体用法去查api就行了。
      

  3.   

    楼上的 人家问得是插入图片,你瞎贴上个图片平铺的代码 也不说明一下...
    主要就是一个g.drawImage();方法。其他的都没用。
    具体用法去查api就行了。
    ----------说的真轻松啊
      

  4.   

    public class ImagePanel extends JPanel {
        //
        private Image image ;
        //
        ImagePanel(Image image) {
           setOpaque(true);        this.image = image;
        }    //
        protected void paintComponent(Graphics g) {
                    super.paintComponent(g);        g.drawImage(...自己查api..
        }}