import  java.awt.*;
  import  java.awt.event.*;
  import  javax.swing.*;
  
  public  class  ButtonImageTest  extends  JPanel{
          static  JFrame  myFrame;
  
          public  ButtonImageTest(){
                  JButton  b;
                  Icon  icon  =  new  ImageIcon("dell.gif");
      b  =  new  JButton("A  Java  Button!",  icon);
                  add(b);
    }
         
  }
  
  //////////另建一个文件
   public  class  B extends  Frame{
   
     public b()
     {
        ButtonImageTest  jt  =  new  ButtonImageTest();
        add(jt);
        setSize(400,250);
        addWindowListener(new  WindowAdapter(){
           public  void  windowClosing(WindowEvent  e)  {
                      System.exit(0);
                          }
                  });
        setVisible(true);
          
     }
       
        public  static  void  main(String[]  args)  
        {
                  myFrame  =  new  JFrame();
                  
        
                  }  
   
   
   }

解决方案 »

  1.   

    ImageIcon  icon  =  new  ImageIcon("dell.gif");
      

  2.   

    还是不出来。
    我的dell.gif和ButtonImageTest.class同一目录。应该不会错吧!
      

  3.   

    找到了解决的办法了
    Image img =  Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("dell.gif"));
    ImageIcon icon = new ImageIcon(img);