0

我正在学习使用 Scene Builder。我已经阅读了一些相同的问题,但我仍然很困惑。

我写了一个css来设置背景,这是我的css代码:

 @charset"UTF-8";
.bodybg{
    -fx-background-image: url('../background.jpg');
    -fx-background-size: 100% 100%; 
}

当我预览我的界面时,它起作用了在此处输入图像描述

但是当我fxml在我的代码中使用时,背景消失了。 在此处输入图像描述

这是我的java代码:

public static void main(String[] args) {

        launch(args);
    }

    @Override
    public void start(Stage primaryStage){
        try {
            // Read file fxml and draw interface.
            Parent root = FXMLLoader.load(getClass().getResource("/demo/StartUI.fxml"));
            Scene scene = new Scene(root);


            primaryStage.setTitle("test");
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();


        } catch(Exception e) {
            e.printStackTrace();
        }
    }
4

1 回答 1

0

您可能没有在 fxml 文件中附加 css 表。现在使用任何文本编辑器(如 Windows 中的记事本)编辑您的 fxml 文件。

在您的 fxml 文件中的子标签之前编写此代码。

<stylesheets>
    <URL value="@your_css_file.css" />
</stylesheets>

这看起来像:

<AnchorPane id="AnchorPane" prefHeight="233.0" prefWidth="481.0" 
styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.102" 
xmlns:fx="http://javafx.com/fxml/1" fx:controller="ate.ChangePasswordController">

<stylesheets>
    <URL value="@your_css_file.css" />
</stylesheets>

   <children>

   </children>
</AnchorPane>
于 2018-09-05T05:31:21.380 回答