import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ghj extends JFrame{
 public ghj(){
JFrame frame=new JFrame("my frame");
Container contentPane=frame.getContentPane();
contentPane.setLayout(new FlowLayout());
ImageIcon icon=createImageIcon("1.gif","ll");
JLabel label=new JLabel("fd",icon,JLabel.CENTER);
JButton button=new JButton("button",buttonicon);
contentPane.add(label,FlowLayout.CENTER);
frame.setSize(100,100);
frame.pack();
    frame.setVisible(true);
}
protected static ImageIcon createImageIcon(String path,String description)
{
java.net.URL imgurl=ghj.class,getResource(path);//出问题了,是jfc swing上的例子,请教                               
if(imgurl!=null)
{return new ImageIcon(imgurl,description);}
else 
{
System.err.println("dfdf"+path);
return null;
}
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run()
             {new ghj();}
             });
        }
}

解决方案 »

  1.   

    是我写的那一句是看了之后想试试的,原先我直接用ImageIcon搞定的,然后这种方法可以检查图片是否存在,就拿来试了,结果不知错在哪里,并不是原文照搬  :(
      

  2.   

    帮帮忙啊!!!!自以为是没有错误的啊
    --------------------Configuration: pk - JDK version 1.4 <Default> - <Default>--------------------
    d:\Program Files\Xinox Software\JCreatorV3\MyProjects\pk\ghj.java:22: ';' expected
            java.net.URL imgurl=ghj.class,getResource(path);
                                                     ^
    d:\Program Files\Xinox Software\JCreatorV3\MyProjects\pk\ghj.java:22: incompatible types
    found   : java.lang.Class
    required: java.net.URL
            java.net.URL imgurl=ghj.class,getResource(path);
                                   ^
    2 errorsProcess completed.
      

  3.   

    楼主,你连逗号和点号都分不清楚么
    另外 buttonicon 这个变量也没有定义
      

  4.   

    java.net.URL imgurl=ghj.class,getResource(path);
    改为:
    java.net.URL imgurl=ghj.class.getResource(path);