用JLabel 中的setIcon(new ImageIcon("path")),将这个lable放到jframe的contentPane或者desktopPane上

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;public class BackgroundPanel extends JFrame
    {
    public BackgroundPanel()
    {
    Container cp = getContentPane();
    NewPanel p = new NewPanel();
    cp.setLayout(new BorderLayout());
    cp.add(p,BorderLayout.CENTER);
    setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args)
    {
    new BackgroundPanel();
    }
    }
    class NewPanel extends JPanel
    {
    public NewPanel()
    {
    }
    public void paintComponent(Graphics g)
    {
    int x = 0;
    int y = 0;
    java.net.URL imgURL = getClass().getResource("ic.gif");
    ImageIcon icon = new ImageIcon(imgURL);
    g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
            while(true)
            {
             g.drawImage(icon.getImage(),x,y,this);
             if(x > getSize().width && y > getSize().height)
             {
             break;
             }
             if(x > getSize().width)
             {
             y += getSize().height;
             x = 0;
             }
             else
             {
             x += getSize().width;
             }
            }
    }
    }
      

  2.   

    JLabel .setIcon(new ImageIcon("path")),将这个lable放到jframe的contentPane或者desktopPane上