急急急急急………………请问JAVA中怎么在JFrame中添加背景图片???
请高手指点!!!

解决方案 »

  1.   

    mport java.awt.*;
    import javax.swing.*;class ImagePanel extends JPanel {
        private Image backgroundImage;
        ImagePanel() {
            // 从系统中加载图片
            backgroundImage = new ImageIcon("image address").getImage();
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            if (backgroundImage != null) {
                g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
            }
        }
    }
      

  2.   


    package cn.gk.com;import javax.swing.JFrame;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import static cn.gk.com.SwingConsole.run;public class StudySystem extends JFrame { /**
     * @param args
     * by Gk
     * 26.07.09
     */

    Container c = this.getContentPane();
    // private MyDialog dlg = new MyDialog(); 
    private JMenu[] menu = {new JMenu("应用学习"), new JMenu("休闲娱乐"), new JMenu("测试检验"),
    new JMenu("网站链接"),new JMenu("功能介绍")};
    private JMenuItem[] items = {
    new JMenuItem("英语学习"),new JMenuItem("飞行棋"),new JMenuItem("听力测试"),new JMenuItem("百度"),
    new JMenuItem("简介"), new JMenuItem("关于") 
    };
    private MyAbout about = new MyAbout(null);
    private IntroFrame inf = new IntroFrame();
    // JLabel lb = new JLabel("欢迎进入多媒体自主学习平台");
    private ImageIcon imageicon;
    private Image image;
    public StudySystem() {
    // c.setLayout(new BorderLayout());
    // add(jp);

    // GirdBagLayout layout = new GridBagLayout();
    // setLayout(new BorderLayout());
    // add(jp,BorderLayout.CENTER);
    // setTitle("多媒体系统");
    imageicon = new ImageIcon("summer.jpg");
    JPanel jp = new JPanel() {
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    //ImageIcon imgIcon = new ImageIcon("summer.jpg");
    image = imageicon.getImage();
    g.drawImage(image,0,0,null);
    }
    };
    add(jp);
    Toolkit kit = Toolkit.getDefaultToolkit();//设置顶层容器为显示屏居中显示
    Dimension screenSize = kit.getScreenSize();
    int width = screenSize.width;
    int height = screenSize.height;
    int x = (width - 500) / 2;
    int y = (height - 500) / 2;
    setLocation(x,y);
    menu[0].add(items[0]);menu[1].add(items[1]);
    menu[2].add(items[2]);menu[3].add(items[3]);
    menu[4].add(items[4]);menu[4].add(items[5]);
    items[4].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    inf.setVisible(true);
    }
    });
    items[5].addActionListener(new GK());
    JMenuBar mb = new JMenuBar();
    for(JMenu jm : menu)
    mb.add(jm);
    mb.setOpaque(false);
    setJMenuBar(mb);

    }


    class GK implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    about.setVisible(true);
    }
    }

    /* public void setBackGround() {
     ((JPanel)this.getContentPane()).setOpaque(false);
       ImageIcon img = new ImageIcon("LD.jpg"); 
       JLabel background = new JLabel(img);
       this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE)); 
       background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); 
    }*/

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    run(new StudySystem(),500,500);
    // new StudySystem();
    }}这是我的源码,MyAbout和IntroFrame是我自己写的两个类。可是图片就是加不进去,请高手指教。
      

  3.   

    可以用1楼的方法来画,也可以用一个大大的jlabel来作为背景(就是利用set imageicon的方法,为label来设置图片),只需要将label放在最下面就可以了
      

  4.   

    imageicon = new ImageIcon("summer.jpg");
    这路径对么?
      

  5.   

    对啊,summer.jpg放在class的文件夹里啊,编译没有出错啊!
      

  6.   

    package qwe;
    import java.awt.event.*;
    import java.awt.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;import javax.swing.*;public class ZhuCe1 extends JFrame {
    private JLabel la_top, la_name, la_IP, la_pwd, la_rpwd; private JTextField t_name, t_IP; private JPasswordField t_pwd1, t_pwd2; private JButton bt1, bt2;
    JLabel lbBg;
    // public void setBack(){
    // ((JPanel) this.getContentPane()).setOpaque(false);
    // ImageIcon img = new ImageIcon(this.getClass().getResource("3.jpg"));
    // JLabel background = new JLabel(img);
    // this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
    // background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
    // }
    // public static void setPositionLeft(JComponent left, JComponent dest,
    int leftMargin, int width, int height) {
    int x = left.getX() + left.getWidth() + leftMargin;
    int y = left.getY();
    dest.setBounds(x, y, width, height);
    } public static void setPositionTop(JComponent top, JComponent dest,
    int topMargin, int width, int height) {
    int x = top.getX();
    int y = top.getY() + top.getHeight() + topMargin;
    dest.setBounds(x, y, width, height);
    } private void setComponentPosition(JPanel p) {

    int txtWidth = 200;
    int txtHeight = 25;
    int lbWidth = 100;
    int lbHeight = 25;
    int topMargin = 10;
    int leftMargin = 20;

    la_name.setHorizontalAlignment(SwingConstants.RIGHT);
    la_name.setBounds(new Rectangle(10,10,lbWidth, lbHeight));

    la_name.setHorizontalAlignment(SwingConstants.RIGHT);
    setPositionLeft(la_name, t_name, leftMargin, txtWidth, txtHeight); la_IP.setHorizontalAlignment(SwingConstants.RIGHT);
    setPositionTop(la_name, la_IP, topMargin, lbWidth, lbHeight);
    setPositionLeft(la_IP, t_IP, leftMargin, txtWidth, txtHeight);
            
    la_pwd.setHorizontalAlignment(SwingConstants.RIGHT);
    setPositionTop(la_IP, la_pwd, topMargin, lbWidth, lbHeight);
    setPositionLeft(la_pwd, t_pwd1, leftMargin, txtWidth, txtHeight);

    la_rpwd.setHorizontalAlignment(SwingConstants.RIGHT);
    setPositionTop(la_pwd, la_rpwd, topMargin, lbWidth, lbHeight);
    setPositionLeft(la_rpwd, t_pwd2, leftMargin, txtWidth, txtHeight); p.add(la_name,null);
    p.add(t_name, null);
    p.add(la_IP, null);
    p.add(t_IP, null);
    p.add(la_pwd, null);
    p.add(t_pwd1, null);
    p.add(la_rpwd, null);
    p.add(t_pwd2, null);
    } public ZhuCe1() {

    ImageIcon titleIcon = new ImageIcon(getClass().getResource("123.png"));
    this.setIconImage(titleIcon.getImage());

    setTitle("IM注册对话框");
    setLayout(new BorderLayout());
    Container c = getContentPane(); JPanel p = new JPanel(new FlowLayout(1));
    JPanel pMain=new JPanel(); la_top = new JLabel("用户注册基本资料:");
    la_name = new JLabel("昵称:");
    la_IP = new JLabel("I P地址:");
    la_pwd = new JLabel("用户密码:");
    la_rpwd = new JLabel("确认密码:");
    t_name = new JTextField(10);
    t_IP = new JTextField(10);
    t_pwd1 = new JPasswordField(10);
    t_pwd2 = new JPasswordField(10);
    bt1 = new JButton("确认注册");
    bt2 = new JButton("取消注册");
    p.add(bt1);
    p.add(bt2);
     
        pMain.setLayout(null);
    setComponentPosition(pMain);
    c.add(BorderLayout.NORTH, la_top);
    c.add(BorderLayout.CENTER, pMain);
    c.add(BorderLayout.SOUTH, p);


    bt2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getActionCommand().equals("取消注册")) {
    System.exit(0);
    }
    }
    }); bt1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    String la_name;
    String la_IP;
    String la_pwd;
    la_name=new String(t_name.getText());
    if(la_name.equals("")){
    JOptionPane.showMessageDialog(null, "用户名不能为空");
    return;
    }
    la_IP=new String(t_IP.getText());
    if(la_IP.equals("")){
    JOptionPane.showMessageDialog(null, "IP不能为空");
    return;
    }
    la_pwd=new String(t_pwd1.getPassword());
    if(la_pwd.equals("")){
    JOptionPane.showMessageDialog(null, "密码不能为空");
    return;
    }
    char[] s1 = t_pwd1.getPassword();
    char[] s2 = t_pwd2.getPassword(); if (String.valueOf(s1).equals(String.valueOf(s2))) {
    JOptionPane.showMessageDialog(null, "恭喜您,注册成功!");
    } else {
    JOptionPane.showMessageDialog(null, "两次输入密码不一致,请重新输入!");
    return;
    }

    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.exit(0);
    }
    Connection con = null;
    Statement s = null;
    ResultSet rs=null; try {
    con = DriverManager
    .getConnection(
    "jdbc:mysql://210.41.34.251?useUnicode=true&characterEncoding=EUC_CN",
    "root", ""); s = con.createStatement();
    s.executeUpdate("insert into user_im (pwd, alias,ip) values ('"
    + t_pwd1.getText().toString()
    + "', '"
    + t_name.getText()
    + "', '"
    + t_IP.getText() + "');");
    System.out.println("sucessfully!");
    rs=s.executeQuery("select user_id from user_im where ip = '"+ t_IP.getText()+"'");
    int id=0;
    while(rs.next()){
     id = rs.getInt("user_id");
    }
    JOptionPane.showMessageDialog(null, "您的ID是:" + id);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    rs.close();
    } catch (Exception e) {
    }
    try {
    s.close();
    } catch (Exception e) {
    }
    try {
    con.close();
    } catch (Exception e) {
    }
    }

    } });

    // this.setBack();
    setVisible(true);
    setSize(400, 230);
    validate();
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    public static void main(String args[]) {
    new ZhuCe1();
    }
    }
      

  7.   

    可以使用相对路径的!
    JPanel jp = new JPanel(){
      public void paintComponent(Graphics g){
        super.paintComponent(g);
        ImageIcon image = new ImageIcon(this.getClass().getResource(
         "src\\...(你相对工程的那个路径)"));
        g.drawImage(image.getImage,0,0,null);
      }
    };
    这样就可以使用相对路径了~~