【发布时间】:2015-09-01 00:55:49
【问题描述】:
当我使用为 ImageIcon 写入的文件名设置面板时,它工作正常:
public TitlePanel(){
setOpaque(false);
setLayout(new BoxLayout( this, BoxLayout.Y_AXIS ));
ImageIcon image = new ImageIcon("images/q0.png");
JLabel imageLabel = new JLabel( image);
add(Box.createRigidArea(new Dimension(150,40)));
add(imageLabel);
}
但是,当我向 ImageIcon 传递一个字符串时,它会停止工作并且没有错误消息。图片只是没有出现,但它打印出正确的字符串路径:
public static String imageName = "\"images/q0.png\"";
public TitlePanel(){
setOpaque(false);
setLayout(new BoxLayout( this, BoxLayout.Y_AXIS ));
ImageIcon image = new ImageIcon(imageName);
System.out.println(imageName);
JLabel imageLabel = new JLabel( image);
add(Box.createRigidArea(new Dimension(150,40)));
add(imageLabel);
}
文件层次结构如下:
- 项目名称
- 源
- 标题面板
- 图片
- 源
有谁知道为什么这会导致 ImageIcon 无法找到该文件?
【问题讨论】:
-
应用程序资源在部署时将成为嵌入式资源,因此明智的做法是立即开始访问它们。 embedded-resource 必须通过 URL 而不是文件访问。请参阅info. page for embedded resource 了解如何形成 URL。
标签: java image swing jlabel imageicon