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