Why isn't my background image being displayed in FXML

百般思念 提交于 2019-12-24 13:31:29

问题


I decided that I wanted to start learning FXML and the first thing that I wanted to do is create a background Image. I've added background images in javafx before and I thought that the process of adding background images in FXML would what somewhat similar to what you would do in javafx. What am I missing?

Here is my FXML File

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox fx:id="menu" spacing = "20" alignment="TOP_CENTER"  xmlns:fx="http://javafx.com/fxml/1" fx:controller="millionairetriviagame.MenulayoutFXMLController">
<StackPane> 
    <ImageView>
        <image>
            <Image url="@ImageFiles/BlueBackgroundColor.jpg" />
        </image>
    </ImageView>
</StackPane>
</VBox>

My main class in javafx

 public class MillionaireTriviaGame extends Application 
{  
@Override
public void start(Stage menuStage) throws Exception 
{
    Parent object = FXMLLoader.load(getClass().getResource("MenulayoutFXML.fxml"));

    Scene menuScene = new Scene(object, 640, 480);

    menuStage.setTitle("Let's play who wants to be a millionaire");
    menuStage.setScene(menuScene);
    menuStage.show();
}

public static void main(String[] args) 
{
    launch(args);
}
}

My project directory(The project that I'm working with is MillionaireTriviaGame)


回答1:


Your project directory shows that your image folder is located in a folder ImageFiles which is not on the classpath. Due to this at runtime, the application is not able to find the image.

Move the folder ImageFiles into src, clean and build the project and try to run again.



来源:https://stackoverflow.com/questions/29550225/why-isnt-my-background-image-being-displayed-in-fxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!