我正在为我的 friend 制作一个基本程序,显示他的这张有趣的照片并循环播放声音。当我导出 JAR 时,它不起作用,所以我用 WinRar 打开 JAR,文件在那里,但它们没有播放
代码:
//imports
public class twerk {
static File fl = new File("res//sounds//liver.au");
static int loo = 1;
static JFrame frame = new JFrame("COLLIN");
public static void frame(){
frame.setSize(1086,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
ImageIcon image = new ImageIcon("res//images//collin.jpg");
JLabel label = new JLabel(image);
frame.add(label, BorderLayout.NORTH);
frame.setIconImage(image.getImage());
frame.setVisible(true);
try
{
String st =fl.getPath();
InputStream in = new FileInputStream(st);
AudioStream au = new AudioStream(in);
AudioPlayer.player.start(au);
if(loo == 1){
Thread.sleep(1900);
frame();
}
} catch(Exception e){}
}
public static void main(String[] args){
frame();
}
}
在 WinRar 中打开的 JAR 文件的图片
因此您可以看到图像和声音文件夹位于 jar 文件中,但它们没有被使用
最佳答案
到部署时,这些资源已变成 embedded-resource 。既然如此,资源必须通过 URL
访问,而不是通过 File
访问。请参阅info page对于标签,对于形成 URL
的方法。
关于路径的注释。根据该 list :
"res//images//collin.jpg"
应该变成:
"/images/collin.jpg"
关于java - 如何导出带有图像和声音的可运行 Jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17322784/