JavaFX 加载图片(使用Intellij IDEA)
【JavaFX 加载图片(使用Intellij IDEA)】主要利用new ImageView(new Image());
进行图片加载
1.通过文件方式输入
try {
FileInputStream file = new FileInputStream(new File("F:/house.png" ));
ImageView imageHouse = new ImageView(new Image(file));
gridpane.add(imageHouse, 0, 0, 1, 2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
2.直接输入url,如果是本地图片,需要在地址前加上 file:
ImageView imageHouse2 = new ImageView(new Image("file:F:/house.png"));
gridpane.add(imageHouse2, 0, 0, 1, 2);
3.加载项目下的图片,注意图片要存放在当前项目下
文章图片
ImageView imageHouse2 = new ImageView(new Image("layoutsamplecss/img/house.png"));
gridpane.add(imageHouse2, 0, 0, 1, 2);
4.利用java类加载器加载
URL url = getClass().getClassLoader().getResource("layoutsamplecss/img/house.png");
ImageView imageHouse2 = new ImageView(url.toExternalForm());
gridpane.add(imageHouse2, 0, 0, 1, 2);
或
URL url = getClass().getResource("/layoutsamplecss/img/house.png");
ImageView imageHouse2 = new ImageView(url.toExternalForm());
gridpane.add(imageHouse2, 0, 0, 1, 2);
推荐阅读
- javaFX的一些坑
- IDEA_WebStorm|Java FX 简介 与 IDEA JavaFX 开发入门
- jxbrowser错误The Chromium profile directory is already used/locked by another BrowserContext instance
- javaFX 8 包和类的统计