JavaFX项目中的CSS错误

Mar*_*sen 3 css java javafx

我正在尝试AnchorPane使用名为的样式表在JavaFX中添加背景图像Style.css

当我运行程序时,我收到以下警告:

警告:com.sun.javafx.css.parser.CSSParser声明CSS错误从javafx.scene.Node$22@5c4a9e8e解析内联样式'AnchorPane':预期的COLON位于[-1,-1]

我的CSS文件如下所示:

#AnchorPane{
    -fx-background-image:url('penthouse.png');
    -fx-background-repeat: no-repeat;
}

.chat{
    -fx-background-image:url('penthouse.png');
    -fx-background-repeat: no-repeat;
}
#btnSend{

}
#txtMessage{

}
#Figur{
    -fx-background-image:url('Figur.png');
}
Run Code Online (Sandbox Code Playgroud)

我的Java代码如下所示:

    public void start(Stage primaryStage) throws Exception {

            BorderPane bp = new BorderPane();

            bp.setRight(createRightOptionPane());
            bp.setBottom(createMessagePane());
            bp.setCenter(createVisualChat());
            Group root = new Group();
            root.getChildren().add(bp);
            Scene scene = new Scene(root);

 // adding the stylesheet to the scene
            scene.getStylesheets().add("Style.css"); 


            primaryStage.setScene(scene);
            primaryStage.setWidth(478);

            primaryStage.setHeight(433);
            primaryStage.setTitle("Chat");
            primaryStage.show();

        }

        private Node createVisualChat() {
            AnchorPane chat = new AnchorPane();
            // setting the anchorPanes ID to AnchorPane
       chat.setStyle("AnchorPane");


            return chat;
        }
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这段代码有什么问题?

Ulu*_*Biy 5

在你的代码中

// setting the anchorPanes ID to AnchorPane
chat.setStyle("AnchorPane");
Run Code Online (Sandbox Code Playgroud)

您正在设置样式而不是ID.它应该是

chat.setId("AnchorPane");
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅使用CSSJavaFX应用程序设置外观.