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

ImageView设置图片

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

ImageView设置图片

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

之后,和之前的fxml一样,得去修改pom.xml,不然maven就会把img这个文件夹的内容全部忽略掉,之后就会找不到图片文件

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

扩展,封装工具类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;
}

使用的时候这样用

@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里面调用

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

}

posted @ 2019-06-08 13:57  Stars-one  阅读(2895)  评论(0编辑  收藏  举报