簡體   English   中英

JavaFX-CSS在我的項目中不起作用,但可以在場景生成器中起作用

[英]JavaFX - css doesn't work in my project, but can work in scene builder

我正在學習使用Scene Builder。我讀過一些相同的問題,但我仍然很困惑。

我寫了一個css來設置背景,這是我的css代碼:

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

當我預覽界面時,它可以正常工作 在此處輸入圖片說明

但是,當我在代碼中使用fxml時,就會出現后台Disapper。 在此處輸入圖片說明

這是我的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();
        }
    }

您可能尚未在fxml文件中附加css表。 現在,使用任何文本編輯器(例如Windows中的記事本)編輯fxml文件。

在您的fxml文件中的children標記之前編寫此代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM