import java.awt.*;
import javax.swing.*;
public class ButtonWithIcon extends JFrame { /**
 * @param args
 */
public ButtonWithIcon(){
this.setTitle("H");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(400,300);
JPanel panel=new JPanel();
//下面是new一个ImageIcon的对象,路径应该没问题
String userDire=System.getProperty("user.dir");
userDire+="2C0398007F41E47FA7A163E6A8399226.gif";
ImageIcon image=new ImageIcon(userDire);
JButton button=new JButton();
button.setIcon(image);
panel.add(button);
this.add(panel);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ButtonWithIcon x=new ButtonWithIcon(); }}
这是我的代码,很简单,就是做了一个带图标的按钮,没有报错,不过为什么运行结果是一个按钮上什么也没有。这是哪里出的问题?我用的eclipse,可能是JDK的问题,或者是eclipse的问题吗??
如果真的是路径的问题,那么~没找的该文件~应该是怎么知道,比如说有抛异常什么的??

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class ButtonWithImageIcon extends JFrame{ /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
     ButtonWithImageIcon aa=new  ButtonWithImageIcon();
     aa.setVisible(true);
    }
    public ButtonWithImageIcon(){
    this.setTitle("Icon");
    this.setSize(400, 400);
    Container con=this.getContentPane();
    con.setLayout(new FlowLayout());
    this.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    JLabel jb=new JLabel("HIII");
    ImageIcon a=new ImageIcon("image/2C0398007F41E47FA7A163E6A8399226.gif");
    jb.setIcon(a);
    con.add(jb);

    con.setBackground(Color.PINK);

    }
    }
    这个是我写的另一个加图标的按钮的代码,那个在学校的一个电脑上是可以显示出来图片的。
    这么说有没有可能是因为eclipse出了什么问题,还是其他什么地方出了什么问题??
    初学者,不大懂,请指教!
      

  2.   

    那个我又试了一下,不用new ImageIcon("image/2C0398007F41E47FA7A163E6A8399226.gif"),改用
    .setIcon(new ImageIcon(getClass().getResource("/image/2C0398007F41E47FA7A163E6A8399226.gif")))
    就可以了,不过这是为什么??那个为什么会出现这种问题?
      

  3.   

    还有啊,原来在按钮上的图标是精致的,不过用.setIcon(new ImageIcon(getClass().getResource("/image/2C0398007F41E47FA7A163E6A8399226.gif"))) 这个方法的图标是可以动的,是用的同一个.gif文件
      

  4.   

    中间少了个分隔符
    import java.io.File;
    import javax.swing.*;
    public class ButtonWithIcon extends JFrame {
        
        /**
         * @param args
         */
        public ButtonWithIcon(){
            this.setTitle("H");
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(400,300);
            JPanel panel=new JPanel();
            //下面是new一个ImageIcon的对象,路径应该没问题
            String userDire=System.getProperty("user.dir");
            userDire+=File.separator; //System.getProperty("user.dir");后面不带分隔符的
            userDire+="2C0398007F41E47FA7A163E6A8399226.gif";
            ImageIcon image=new ImageIcon(userDire);
            JButton button=new JButton();
            button.setIcon(image);
            panel.add(button);
            this.add(panel);
            this.setVisible(true);
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ButtonWithIcon x=new ButtonWithIcon();
            
        }
        
    }
      

  5.   

    嗯,我把那个getClass().getResource("/image/2C0398007F41E47FA7A163E6A8399226.gif").toString();//就是变成路径又传进取了还是不行。
    然后把System.getProperty("user.dir")   还有  getClass().getResource("/image/2C0398007F41E47FA7A163E6A8399226.gif").toString() 的都print();出来对比了一下好像是一个是……/bin/image/2C0398007F41E47FA7A163E6A8399226,一个是src/image/2C0398007F41E47FA7A163E6A8399226什么的,
    为什么在同一个包里面不能只写一个相对路径呢?就是只写("/image/2C0398007F41E47FA7A163E6A8399226.gif").
      

  6.   

    我看你的类没有包声明的,应该是放在默认包下的吧。
    从逻辑上说类与图片不是在同一个包中。
    如果都是在img包下的话getClass().getResource("2C0398007F41E47FA7A163E6A8399226.gif")就可以了