我已经将onAction方法添加到FileChooser的图像视图中。单击后,应将ImageView图像替换为新选择的图像。
package sample;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import java.io.File;
public class Controller {
public ImageView image;
public void event() {
ExtensionFilter imageFilter = new FileChooser.ExtensionFilter("Image Files", "*.jpg", "*.jpeg", "*.png", "*.gif");
FileChooser fc = new FileChooser();
fc.getExtensionFilters().add(imageFilter);
File file = fc.showOpenDialog(Main.thisStage);
setImage(file.getAbsolutePath());
}
private void setImage(String url) {
image.setImage(new Image(url));
}
}
但我正面临此错误:
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:620)
at sample.Controller.setImage(Controller.java:22)
at sample.Controller.event(Controller.java:19)