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

}
相關文章
相關標籤/搜索