求教:java ImageIcon显示不出来

kpym 2013-09-02 06:54:04
用eclipse编写,代码如下:
package logframe;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class log extends JFrame implements ActionListener{

/**
* @param args
*/
ImageIcon bj = new ImageIcon("images/log.jpg"); //定义图标并截入图片 用这个路径就显示不出来
////定义图标并截入图片 D:/workspace/logtest/src/images/log.jpg(用这个路径就能显示)

JLabel lbimg = new JLabel(bj); //定义标签并载入图标
JButton btnlog = new JButton("登录");
JPanel jp = new JPanel(); //定义一个panel
public log() { //类构造方法
setTitle("登录界面");
this.setVisible(true);
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbimg.setBounds(0, 0, bj.getIconWidth(), bj.getIconHeight()); //标签填满整个面板
this.getLayeredPane().add(lbimg,new Integer(Integer.MIN_VALUE)); //frame第二层添加标签
}
private void buju() {
// TODO Auto-generated method stub
jp = (JPanel)this.getContentPane(); //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
jp.setOpaque(false); //将panel设置成透明,这样才能显示下面一层的标签图片
jp.setLayout(null); //无布局

btnlog.setSize(120,36); //按钮大小
btnlog.setLocation(100,50); //按钮位置
jp.add(btnlog); //panel添加按钮
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
log a = new log();
a.buju();
a.show();
}

}

以下是文件夹路径,图片是放在images下。



用绝对路径显示图片没问题,但用相对路径就显示不出来了,请教大神。



...全文
853 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
suchaochen 2016-04-18
  • 打赏
  • 举报
回复
也可以使用 new ImageIcon(String filename) 构造方法。 1)如果使用相对路径,起点是当前工程的根目录,而不是当前包下 2)当然使用绝对路径肯定没有错
kpym 2013-09-04
  • 打赏
  • 举报
回复
引用 6 楼 huntor 的回复:
相对路径是相对user.dir这个目录而言,就是你启动Java程序时的目录,在eclipse里就是项目的根目录。 可以使用ClassLoader的getResource方法。 new ImageIcon(getClass().getClassLoader().getResource("/images/log.jpg"));
把第 一个“/”去掉,改成: ImageIcon bj = new ImageIcon(getClass().getClassLoader().getResource("images/log.jpg")); 运行,OK!
huntor 2013-09-03
  • 打赏
  • 举报
回复
相对路径是相对user.dir这个目录而言,就是你启动Java程序时的目录,在eclipse里就是项目的根目录。 可以使用ClassLoader的getResource方法。 new ImageIcon(getClass().getClassLoader().getResource("/images/log.jpg"));
  • 打赏
  • 举报
回复
引用 4 楼 kpym002 的回复:
[quote=引用 3 楼 xxhhbb1538 的回复:] [quote=引用 2 楼 kpym002 的回复:] [quote=引用 1 楼 xxhhbb1538 的回复:] 相对路径是相对当前的路径,你那个路径表示images文件夹在当前类同一个文件夹,在images前加个斜杠 /images/log.jpg
试过了,不行ImageIcon bj = new ImageIcon("/images/log.jpg"); 其实也相不相对也无所谓,就是如果我的整个源码都移动了位置,也可以用,这就行了。[/quote] 在前面再加两点呢 ImageIcon bj = new ImageIcon("../images/log.jpg"); [/quote] 不行哦 ImageIcon bj = new ImageIcon("../images/log.jpg");[/quote]
kpym 2013-09-02
  • 打赏
  • 举报
回复
引用 3 楼 xxhhbb1538 的回复:
[quote=引用 2 楼 kpym002 的回复:] [quote=引用 1 楼 xxhhbb1538 的回复:] 相对路径是相对当前的路径,你那个路径表示images文件夹在当前类同一个文件夹,在images前加个斜杠 /images/log.jpg
试过了,不行ImageIcon bj = new ImageIcon("/images/log.jpg"); 其实也相不相对也无所谓,就是如果我的整个源码都移动了位置,也可以用,这就行了。[/quote] 在前面再加两点呢 ImageIcon bj = new ImageIcon("../images/log.jpg"); [/quote] 不行哦 ImageIcon bj = new ImageIcon("../images/log.jpg");
  • 打赏
  • 举报
回复
引用 2 楼 kpym002 的回复:
[quote=引用 1 楼 xxhhbb1538 的回复:] 相对路径是相对当前的路径,你那个路径表示images文件夹在当前类同一个文件夹,在images前加个斜杠 /images/log.jpg
试过了,不行ImageIcon bj = new ImageIcon("/images/log.jpg"); 其实也相不相对也无所谓,就是如果我的整个源码都移动了位置,也可以用,这就行了。[/quote] 在前面再加两点呢 ImageIcon bj = new ImageIcon("../images/log.jpg");
kpym 2013-09-02
  • 打赏
  • 举报
回复
引用 1 楼 xxhhbb1538 的回复:
相对路径是相对当前的路径,你那个路径表示images文件夹在当前类同一个文件夹,在images前加个斜杠 /images/log.jpg
试过了,不行ImageIcon bj = new ImageIcon("/images/log.jpg"); 其实也相不相对也无所谓,就是如果我的整个源码都移动了位置,也可以用,这就行了。
  • 打赏
  • 举报
回复
相对路径是相对当前的路径,你那个路径表示images文件夹在当前类同一个文件夹,在images前加个斜杠 /images/log.jpg

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧