Open Image from FileChooser in JavaFX

前端 未结 1 1824
长发绾君心
长发绾君心 2021-01-16 14:05

My program is supposed to upload a image from a file and then it displays that image as the background. My problem is that when I create an Image object in it\'

1条回答
  •  失恋的感觉
    2021-01-16 14:42

    The problem is that the constructor of Image is expecting a String url, whereas you're passing it a File. Any good IDE will tell you what a given method is expecting as its parameters; find that keyboard shortcut and use it (Ctrl + P in IntelliJ). From there, all you have to do is find a way to convert a File to a Stringrepresenting its url. In this case:

    Image image1 = new Image(file.toURI().toString());
    

    Note that you are never actually setting your background image, you need to add the following line to your lambda:

    stac.setBackground(new Background(backgroundImage));
    

    For this though, you will have to move the declaration of stac above your action listener.

    0 讨论(0)
提交回复
热议问题