从外部资源加载Javafx中的图像

从外部资源加载Javafx中的图像,java,resources,javafx,Java,Resources,Javafx,我想使用javafx从目录(Jar文件外部)加载一个图像,即/Users/ossama/Projects/Pi/javafx/rajertime/backgroundBlue wallper.jpg,但是没有加载图像。 我猜是因为下面的代码将从jar文件内部获取图像 需要帮忙吗 images = new ArrayList<String>(); directory = new File("/Users/ossama/Projects/Pi/javafx/prayertime/back

我想使用javafx从目录(Jar文件外部)加载一个图像,即
/Users/ossama/Projects/Pi/javafx/rajertime/backgroundBlue wallper.jpg
,但是没有加载图像。 我猜是因为下面的代码将从jar文件内部获取图像

需要帮忙吗

images = new ArrayList<String>();
directory = new File("/Users/ossama/Projects/Pi/javafx/prayertime/background/");
File[] files = directory.listFiles();
for(File f : files) {images.add(f.getName());}   
System.out.println(images);
int countImages = images.size();

int imageNumber = (int) (Math.random() * countImages);
rand_Image_Path = directory + images.get(imageNumber);

String image = JavaFXApplication4.class.getResource(rand_Image_Path).toExternalForm(); 
Mainpane.setStyle("-fx-background-image: url('" + image + "'); -fx-background-image-repeat: repeat; -fx-background-size: 1080 1920;-fx-background-position: bottom left;");
images=newarraylist();
directory=新文件(“/Users/ossama/Projects/Pi/javafx/prayertime/background/”;
File[]files=目录.listFiles();
对于(文件f:files){images.add(f.getName());}
System.out.println(图像);
int countImages=images.size();
int imageNumber=(int)(Math.random()*countImages);
rand\u Image\u Path=directory+images.get(imageNumber);
String image=JavaFXApplication4.class.getResource(rand_image_Path).toExternalForm();
Mainpane.setStyle(“-fx背景图像:url(“+image+”);-fx背景图像重复:重复;-fx背景大小:1080 1920;-fx背景位置:左下;”);

尝试使用新文件(rand_Image_path).toURI().toul().toString()您的意思是:尝试{String Image=new File(rand_Image_path).toURI().toul().toString Image=JavaFXApplication4.class.getResource(rand_Image_path).toExternalForm()//Mainpane.setStyle(“-fx背景图像:url(“+Image+”);-fx背景重复:重复;”;主窗格.setStyle(“-fx背景图像:url(“+image+”);-fx背景图像重复:重复;-fx背景大小:1080 1920;-fx背景位置:左下;”;)catch(IOException e){logger.warn(“意外错误”,e);}不,不使用JavaFXApplication,只是文件#toURI#toURLsorry,但这是我使用的,但仍然得到null异常{String image=new File(rand#u image_Path).toURI().toURL().toString();Mainpane.setStyle(“-fx background image:url(“+image+”);-fx background image repeat:repeat;-fx背景大小:1080 1920;-fx背景位置:左下;”)}catch(IOException e){logger.warn(“意外错误”,e);}