Why isn't my background image being displayed

2019-08-08 16:13发布

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)

Project Directory

1条回答
Melony?
2楼-- · 2019-08-08 16:54

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.

查看更多
登录 后发表回答