这是个用Swing写的QQ界面,可是图片一直显示不了,图片放在项目文件下img文件夹下,求高手帮忙解决啊!
/*
 * TestQQ类将用于建立QQ登录界面
 * 
 * 2008.5.16
 */package mypro;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class QQTest extends JFrame {
Container contentPane;
    ImageIcon img = new ImageIcon("img/1111.gif");
    
    JPanel paneTop = new JPanel();
    JPanel paneMid = new JPanel();
    JPanel paneBut = new JPanel();
    JPanel paneAll = new JPanel();
    
    JLabel lblTop = new JLabel();
    JLabel lblName = new JLabel();
    JLabel lblPwd = new JLabel();
    JLabel lblApply = new JLabel();
    JLabel lblForget = new JLabel();
    JLabel lblModel = new JLabel();
    JLabel lblNull = new JLabel();
    JLabel lblNull1 = new JLabel();
    
    JTextField txtName = new JTextField(15);
    JPasswordField txtPwd = new JPasswordField(15);
    
    JComboBox cmb = new JComboBox();
    
    JCheckBox chk = new JCheckBox();
    
    JButton btnKill = new JButton("查杀木马");
    JButton btnSet = new JButton("设置");
    JButton btnLogin = new JButton("登录");
    
    
    
    public QQTest(){
        lblTop.setIcon(img);//顶部添加图片
        paneTop.add(lblTop);
        
        lblName.setText("QQ帐号:");//中部添加非按钮控件
        lblApply.setText("申请帐号   ");
        lblPwd.setText("QQ密码:");
        lblForget.setText("忘记密码?");
        lblModel.setText("状态:");
        
        String[] s1 = {"隐身","在线","忙碌"};
        cmb.addItem(s1[0]);
        cmb.addItem(s1[1]);
        cmb.addItem(s1[2]);
        
        chk.setText("自动登录");
        
        paneMid.add(lblName);
        paneMid.add(txtName);
        paneMid.add(lblApply);
        
        paneMid.add(lblPwd);
        paneMid.add(txtPwd);
        paneMid.add(lblForget);
        
        paneMid.add(lblModel);
        paneMid.add(cmb);
        paneMid.add(chk);
        
        paneBut.add(btnKill);//底部添加按钮控件
        paneBut.add(btnSet);
        paneBut.add(lblNull1);
        paneBut.add(btnLogin);
        
        contentPane = this.getContentPane();
        
        contentPane.add(paneTop,BorderLayout.NORTH);//使用BorderLayout布局顶部添加在North
        contentPane.add(paneMid,BorderLayout.CENTER);//中部添加非按钮控件
        contentPane.add(paneBut,BorderLayout.SOUTH);//底部添加按钮控件
                  
        setTitle("欢迎使用QQ");
        setSize(300,240);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - getSize().width)/2,(screen.height - getSize().height)/2 );
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }
    public static void main(String args[]){
        QQTest d = new QQTest();
    }
}

解决方案 »

  1.   


      可能是你的目录写错了  "./img/1111.gif"  试试  .表示当前目录  要不然你要把目录写全
      

  2.   

    ImageIcon img = new ImageIcon("img/1111.gif");路径不对,你用全路径试试看,而且路径格式是这样的"D:\\img\\1111.gif"
      

  3.   


     2楼   它的 路径格式那样写是没有问题的  你的也对  两种都行  楼主 只要把    ImageIcon img = new ImageIcon("img/1111.gif");  替换成  ImageIcon img = new ImageIcon(".img/1111.gif");
     
     相信就OK了
      

  4.   

    lblTop.setIcon(img);这句后面添加一句
    img.setImageObserver(lblTop);看看
      

  5.   

    LZ确定一下1111.gif的位置└─root
        │  QQTest.class
        │
        └─img
                1111.gif
      

  6.   

    ImageIcon img = new ImageIcon("img/1111.gif"); 路径不对,你用全路径试试看,而且路径格式是这样的"D:\\img\\1111.gif"
      

  7.   

    你的工程里有src目录么。。如果有的话,你把你那个img/1111.gif放在src外面看看
      

  8.   

    ImageIcon img = new ImageIcon("img/1111.gif"); 换成ImageIcon img = new ImageIcon("img//1111.gif");
    把你的img文件夹放在你的工程里面啊 应该就可以了 还有看下你的图片的格式是不是。gif
      

  9.   

    ImageIcon img = new ImageIcon("img\1111.gif"); 换成ImageIcon img = new ImageIcon("img\\1111.gif"); 
    把你的img文件夹放在你的工程里面啊 应该就可以了 还有看下你的图片的格式是不是。gif
      

  10.   

     
     我已经测试过了 其他地方是没有问题的
            
              ImageIcon img = new ImageIcon("img/1111.gif");
             改成  ImageIcon img = new ImageIcon(".img/1111.gif");   多一个点 他表示 当前项目所在目录           除此外你还要确认几个地方  1、你的img文件夹是在项目的根目录吗
                                         2、图片名 尤其是后缀  你要是模仿 QQ登录窗口 那图片应该是静态  怎么要用  gif     我在Eclipse中测试过你的程序  ImageIcon img = new ImageIcon("./image/login.jpg"); // image 是我项目下的子文件夹 login.jpg 是一张图片  结果一切正常。