ImageView Picture
JavaFx of ImageView, set the picture can not be set directly through the property, can only be set by the code
ImageView Picture
First of all, we let the controller fxml corresponding java file implements Initializable
the interface, and then it is set in the initialize method of the interface replication of our ImageView picture
My picture is on an img folder
after, and the same as the previous fxml , you have to modify pom.xml
, or img maven will put all the contents of a folder ignored, then you will not find the picture file
@Override
public void initialize(URL location, ResourceBundle resources) {
//设置图片,inPathImg是ImageView
Image image = new Image(getClass().getResource("img/file.png").toString());
inPathImg.setImage(image);
}
Extension, packaging tools PathUtil
Although the above is successfully set up the picture, but each also wrote trouble, so I encapsulate a class to quickly get the picture
/**
* 获得图片文件,
* @param o 当前的class,传入this即可
* @param fileName 图片名+扩展名
* @return 图片image
*/
public static Image getImg(Object o, String fileName) {
URL res = o.getClass().getResource("img");
if (fileName.contains(".")) {
String temp = res.toString() + "/" + fileName;
return new Image(temp);
}
return null;
}
When using such a
@Override
public void initialize(URL location, ResourceBundle resources) {
//设置图片
inPathImg.setImage(PathUtil.getImg(this, "file.png"));
outPathImg.setImage(PathUtil.getImg(this, "file.png"));
}
Extension tools obtained fxml file path
Originally, when the test is no problem, but if the project is packaged as a jar package, after opening will error.
Online search for information, the original jar package can not be used directly File this class, in order to use the jar package inside the file, ways have to use IO streams
/**
* 获得fxml文件路径
* @param o class文件,传入this
* @param fileName 文件名
* @return
*/
public static URL getFxmlPath(Object o,String fileName) {
return o.getClass().getResource("fxml/"+fileName+".fxml");
}
/**
* 获得文件
* @param Object o this
* @param String fileName 文件名
*/
public static InputStream getFxmlFile(Object o,String fileName) {
return o.getClass().getResourceAsStream("fxml/"+fileName+".fxml");
}
Main calls inside
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(); // 创建对象
loader.setBuilderFactory(new JavaFXBuilderFactory()); // 设置BuilderFactory
loader.setLocation(PathUtil.getFxmlPath(this, "scene_main"));//获得fxml的路径
InputStream inputStream = PathUtil.getFxmlFile(this, "scene_main");//加载jar包中的fxml文件
Object o = loader.load(inputStream);
//这是之前使用的方式,使用的是FXMLLoader的静态方法,如果使用jar包的方式,则会报错
//Parent root = FXMLLoader.load(PathUtil.getFxmlPath(this,"scene_main"));
Parent root = (Parent) o;
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
PathUtil source
package wan.Utils;
import java.io.InputStream;
import java.net.URL;
import javafx.scene.image.Image;
/**
* @author StarsOne
* @date Create in 2019/6/5 0005 14:01
* @description
*/
public class PathUtil {
/**
* 获得图片文件,
* @param o 当前的class,传入this即可
* @param fileName 图片名+扩展名
* @return 图片image
*/
public static Image getImg(Object o, String fileName) {
URL res = o.getClass().getResource("img");
if (fileName.contains(".")) {
String temp = res.toString() + "/" + fileName;
return new Image(temp);
}
return null;
}
/**
* 获得fxml文件路径
* @param o class文件,传入this
* @param fileName 文件名
* @return
*/
public static URL getFxmlPath(Object o,String fileName) {
return o.getClass().getResource("fxml/"+fileName+".fxml");
}
public static InputStream getFxmlFile(Object o,String fileName) {
return o.getClass().getResourceAsStream("fxml/"+fileName+".fxml");
}
}