【问题标题】:Why isn't my background image being displayed in FXML为什么我的背景图片没有以 FXML 显示
【发布时间】:2015-06-15 12:15:55
【问题描述】:

我决定开始学习 FXML,我想做的第一件事就是创建背景图像。我之前在 javafx 中添加了背景图像,我认为在 FXML 中添加背景图像的过程与您在 javafx 中所做的有点相似。我错过了什么?

这是我的 FXML 文件

<?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>

我在 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);
}
}

我的项目目录(我正在使用的项目是 MillionaireTriviaGame)

【问题讨论】:

    标签: java image background javafx fxml


    【解决方案1】:

    您的项目目录显示您的图像文件夹位于文件夹ImageFiles 中,该文件夹不在类路径中。由于这个在运行时,应用程序无法找到图像。

    将文件夹ImageFiles 移动到src,清理并构建项目并尝试再次运行。

    【讨论】:

    • 你不能在 FXML 中做到这一点?我以前在 CSS 中做过,但我想尝试在 FXML 中做到这一点
    • 你可以,你能添加你的图像和fxml的位置吗?
    • 当然我会把截图贴在项目目录上。
    • 我已经添加了我的项目目录
    • 嘿,伙计,最后一件事。我将音频文件文件夹添加到图像文件夹所在的同一位置。我尝试加载像这样的 mp3 音频文件 但这不起作用。我能做什么?
    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多