Java开发桌面程序学习(七)——ImageView设置图片以及jar包读取fxml文件

ImageView设置图片

JavaFx的ImageView,设置图片不能直接经过属性设置,只能经过代码来设置java

ImageView设置图片

首先,咱们让fxml对应的那个controller的java文件实现Initializable接口,以后就在复写的该接口的initialize方法中设置咱们ImageView的图片
个人图片是放在了一个img文件夹里

以后,和以前的fxml同样,得去修改pom.xml,否则maven就会把img这个文件夹的内容所有忽略掉,以后就会找不到图片文件
maven

@Override
public void initialize(URL location, ResourceBundle resources) {
    //设置图片,inPathImg是ImageView
    Image image = new Image(getClass().getResource("img/file.png").toString());
    inPathImg.setImage(image);
}

扩展,封装工具类PathUtil

上面的虽然是成功设置了图片,可是每次这样写也是麻烦,因此我就封装了一个类来快速获得图片ide

/**
* 得到图片文件,
* @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;
}

使用的时候这样用工具

@Override
public void initialize(URL location, ResourceBundle resources) {
    //设置图片
    inPathImg.setImage(PathUtil.getImg(this, "file.png"));
    outPathImg.setImage(PathUtil.getImg(this, "file.png"));
}

扩展,工具类得到fxml文件路径

本来,测试的时候是没有问题的,可是,若是是项目封装成jar包,以后打开就会报错。
网上查了资料,原来是jar包中不能直接使用File这个类,要想使用jar包里面的文件,得使用IO流的方式测试

/**
 * 得到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里面调用ui

@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源码

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");
    }

}
相关文章
相关标签/搜索