如何在java应用程序中 加载背景图片啊?
 谢谢大家了!

解决方案 »

  1.   

    class topPanel extends JPanel{
        private Image img;
        XYLayout xYLayout = new XYLayout();
        public topPanel(){
            try{
                File f=new File("images\\top.jpg");
                img=ImageIO.read(f);
            }catch(Exception e){
                System.out.println("not find file");
            }
            this.setLayout(xYLayout);
        }
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(img,0,0,360,50,null);
        }
    }
      

  2.   

    (转)
    getContentPane().add(bgLabel,-1);                // 在最底层加一个带背景图的JLabel
    getContentPane().add(yourContentPanel,0);        // 在顶层添加你的组件如果添加的组件是JPanel,要把JPanel设置为透明的。panel.setOpaque(false);
      

  3.   

    这是我在网上找到的,大家看看,我也是个超级菜鸟
    import java.awt.Graphics;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class TestBackground extends JFrame{
            ImageIcon img;        public TestBackground(){
                    img = new ImageIcon("D:/temp/Winter.jpg");                JPanel panel = new JPanel(){
                            public void paintComponent(Graphics g){
                                    g.drawImage(img.getImage(), 0, 0, null);
                                    super.paintComponent(g);
                            }
                    };                panel.setOpaque(false);
                    panel.add( new JButton( "Hello" ) );
                    setContentPane( panel );
            }        public static void main(String [] args){
                    TestBackground frame = new TestBackground();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300, 300);
                    frame.setVisible(true);
            }
    }
      

  4.   

    这是我自己写的一个可以设背景图的Panel类,楼主参考一下呀
    package gui;import java.io.*;
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.*;/**
     * 这个类继承了JPanel类
     * 此类有一个addBackgroundImage()的方法
     * 是为了给此JPanel加一个背景,参数是背景图片的本地路径
     * @author 樊鹏飞
     * time 2005-9-20
     */
    public class ImagePane extends JPanel{
    private Image image=null;
    private int top=0;
    private int left=0;
    private int bottom=0;
    private int right=0;
    /**
     * 这一个零参数的构造方法
     *
     */
    public ImagePane(){
    super();
    }
    /**
     * 此方法是给该类加一个背景图案
     * @param imagePath 背景图案的本地路径字符串,注意一定要用相对路径
     */
    public void addBackgroundImage(String imagePath){
    try{
        image=ImageIO.read(new File(imagePath));
        
    }catch(IOException err){

    }catch(Exception err){

    }

    }
    /**
     * 此方法重写父类的方法
     * 把图片画到此类,并自动涮新
     */
    public void paintComponent(Graphics g){
    g.drawImage(image,0,0,this.getWidth(),this.getHeight(),null);
    }
    /**
     * 重新父类的方法
     * 为了给面板四周留下空隙
     * 为了界面更人性化更美观
     */
    public Insets getInsets(){
    return new Insets(top,left,bottom,right);
    }
    /**
     * 此方法是设置面板四周留的空隙
     * 以像素为单位
     * @param top 上边留的空隙像素
     * @param left 左边留的空隙像素
     * @param bottom 下边留的空隙像素
     * @param right 右边留的空隙像素
     */
    public void setInsets(int top,int left,int bottom,int right){
    this.top=top;
    this.left=left;
    this.bottom=bottom;
    this.right=right;
    this.getInsets();
    }
    /**
     * 用于测试此类的main方法
     * @param args
     */
    /*public static void main(String [] args){
    JFrame frame=new JFrame();
    frame.setTitle("TEST");

    frame.setSize(400,300);
    ImagePane imagePane=new ImagePane();
    imagePane.addBackgroundImage("file/test.jpg");
    ImageIcon icon=new ImageIcon("file/woman.jpg","button");

    //JButton button=new JButton("OK");
    //button.setIcon(icon);
    ImageButton button=new ImageButton("OK","file/woman.jpg");
    imagePane.setInsets(6,6,6,6);
    imagePane.setLayout(new BorderLayout());
    imagePane.add(button,BorderLayout.SOUTH);
    frame.add(imagePane);

    frame.setVisible(true);
    }*/}
      

  5.   

    //package test.International.chinajavaworld;import javax.swing.JFrame;
    import java.net.URL;
    import javax.swing.ImageIcon;
    import java.awt.MediaTracker;
    import java.awt.Image;
    import java.net.MalformedURLException;
    import java.awt.GridLayout;
    import javax.swing.JDialog;
    import java.io.File;public class TestPaintPanel  {
      public static void main(String[] args) {
        JFrame fr = new JFrame();
        fr.setTitle("GIFT-PaintPanel-演示载入图片的方法");
        //String urlstr = "http://photo.sohu.com/20040823/Img221677764.jpg";
        String filestr="D://a.jpg";
    //如果是自己的机器上...un comment following......
       //String urlstr="file:///D://yxmm1051.jpg";
      // G:\下载文件夹\PaintPanel
       String urlstr="file:///G://下载文件夹//PaintPanel//yxmm1051.jpg";
        URL url = null;
        try {
          url = new URL(urlstr);
        }
        catch (MalformedURLException ex) {
        }    ImageIcon icon = new ImageIcon(url);    //////////////////loadimage//////////////////////
        Image image = fr.getToolkit().getImage(url);
        MediaTracker tracker = new MediaTracker(fr);
        tracker.addImage(image, 0);
        try {
          tracker.waitForID(0);
        }
        catch (InterruptedException ie) {}
        ////////////////////////////////////////////////
        fr.getContentPane().setLayout(new GridLayout(2, 2));
        fr.setSize(500, 600);    fr.getContentPane().add(new PaintPanel(image));
        fr.getContentPane().add(new PaintPanel(urlstr));
        fr.getContentPane().add(new PaintPanel(icon));
        fr.getContentPane().add(new PaintPanel(url));
        //this is a litter different...
        JDialog dialog = new JDialog(fr, "GIFT-演示让图片成为背景", true);
        //本机上的文件...
        dialog.getContentPane().add(new PaintPanel(new File(filestr)));
        dialog.setSize(200, 200);    fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        fr.setVisible(true);
        dialog.setVisible(true);
        fr.validate();
      }}
    //package test.International.chinajavaworld;
    /**
     * <p>Title: PaintPanel</p>
     * <p>Description:此程序演示如何载入图片,并让其作为panel的背景</p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: gift2u</p>
     * @author liwu chinajavaworld
     * @version 1.0
     */
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.net.URL;
    import java.net.*;import java.awt.MediaTracker;
    import java.io.File;
    import javax.imageio.ImageIO;
    import java.io.*;
    import java.awt.Toolkit;public class PaintPanel
        extends JPanel {
      Image image = null;  /**
       * PaintPanel
       * 外部给图片,直接载入
       * @param image Image
       */
      public PaintPanel(Image image) {
        this.image = image;
      }
      /**
       * PaintPanel
       * 外部给出file引用,通过ImageIO载入
       * @param file File
       */
      public PaintPanel(File file) {
        try {
          Image readImage = ImageIO.read(file);
          this.image = readImage;
        }
        catch (IOException ex) {
        }
      }  /**
       * PaintPanel
       *外部给出string路径,通过Toolkit载入
       * @param string String
       */
      public PaintPanel(String string) {
        URL url = null;
        try {
          url = new URL(string);
        }
        catch (MalformedURLException ex) {
        }
        image = Toolkit.getDefaultToolkit().getImage(url);    MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(image, 0);
        try {
          tracker.waitForID(0);
        }
        catch (InterruptedException ie) {
        }  }  /**
       * PaintPanel
       *外部给出ImageIcon,利用ImageIcon载入
       * @param icon ImageIcon
       */
      public PaintPanel(ImageIcon icon) {
        this.image = icon.getImage();
      }  /**
       * PaintPanel
       * 外部给出URL,利用ImageIcon载入
       * @param icon url
       */
      public PaintPanel(URL url) {
        ImageIcon icon = new ImageIcon(url);
        this.image = icon.getImage();
      }  public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        if (image != null) {
          g2d.drawImage(image, 0, 0, this);
        }
      }
    }