gpt4 book ai didi

image - 通过代码在ImageView中加载图片

转载 作者:行者123 更新时间:2023-12-02 21:36:42 24 4
gpt4 key购买 nike

我已经使用 javafx 的 scenebuilder 构建了我的应用程序。我有一个表格,人们必须上传图像。我使用了这段代码

public void photoChooser(ActionEvent evt) {
System.out.println("photoChooser method is called");
try{
FileChooser fileChooser= new FileChooser();
fileChooser.setTitle("Choose a file");
File file = fileChooser.showOpenDialog(stagehere);
if(file != null){
System.out.println(file);
String img = file.toString();
//Image image = new ImageIcon(img);

try{

// image= new Image();
Image image = new Image(img);

} catch (Exception e) {System.out.println("Can't upload image " + e);}


//employeeImage.setImage(image);
try{
// employeeImage.setImage(image);
} catch(Exception e){System.out.println("Can't set the image" + e);}
employeeImage.setFitWidth(150);
employeeImage.setFitHeight(150);
}

我收到了这个错误调用 photoChooser 方法
A:\images\fb\status\asd.jpg
无法上传图像java.lang.IllegalArgumentException:无效的URL:未知协议(protocol):a

最佳答案

Image 的构造函数需要一个 URL,而不是文件路径。因此,如果字符串中存在“:”,则到此为止的所有内容都将被解释为协议(protocol)(通常类似于 httpfileftp).

你必须换行

String img = file.toString();

String img = file.toURI().toURL().toExternalForm();

这会在转换为字符串之前从文件中获取 URL。我首先转换为 URI,因为 File.toURL 已弃用,这是建议的“解决方法”。

关于image - 通过代码在ImageView中加载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21215299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com