JavaFX BorderPane不会采用背景色

JavaFX BorderPane不会采用背景色,javafx,borderpane,Javafx,Borderpane,我试图通过遵循Oracle提供的教程自学基本的JavaFX 在BorderPane教程()中,它指定了背景色 这是我的代码片段: /** * This Method creates and defines a horizontal box with a button. */ public HBox addHorizontalBoxWithButton() { // set up horizontal box and button HBox hBox = new HBox();

我试图通过遵循Oracle提供的教程自学基本的JavaFX

在BorderPane教程()中,它指定了背景色

这是我的代码片段:

/**
 * This Method creates and defines a horizontal box with a button.
 */
public HBox addHorizontalBoxWithButton() {
    // set up horizontal box and button
    HBox hBox = new HBox();
    hBox.setPadding(new Insets(10, 10, 10, 10));
    hBox.setSpacing(10);
    hBox.setStyle("-fx-background-colour: #FFFFFF;");
    hBox.setAlignment(Pos.CENTER);
    Button startButton = new Button("CLICK ME");
    startButton.setPrefSize(100, 30);
    // set up a message
    Text message = new Text("Click the button to get started.");
    message.setId("message");

    hBox.getChildren().add(message);
    hBox.getChildren().add(startButton);

    return hBox;
}
我试过各种不同的背景色,但都不管用。我是不是遗漏了什么


另外,我使用的是一个.css文件,但它只为“消息”添加了样式。

好的,我刚刚解决了这个问题

我更改了代码,如下所示:

/**
 * This Method creates and defines a horizontal box with a button.
 */
public HBox addHorizontalBoxWithButton() {
    // set up horizontal box and button
    HBox hBox = new HBox();
    hBox.setId("hBox");
    hBox.setPadding(new Insets(10, 10, 10, 10));
    hBox.setSpacing(10);
    // hBox.setStyle("-fx-background-colour: #FFFFFF;");
    hBox.setAlignment(Pos.CENTER);
    Button startButton = new Button("CLICK ME");
    startButton.setPrefSize(100, 30);
    // set up a message
    Text message = new Text("Click the button to get started.");
    message.setId("message");

    hBox.getChildren().add(message);
    hBox.getChildren().add(startButton);

    return hBox;
}
我在.css文件中添加了以下内容:

#hBox {
-fx-background-color: linear-gradient(#04B45F, #81F79F);
}

原始代码的唯一问题是,您的样式设置中存在“打字错误”(英语化?)。应该是

hBox.setStyle("-fx-background-color: #FFFFFF;");
不是

将外部样式表与

#hbox {
    -fx-background-color: red ;
}
是比使用内联样式更好的解决方案

#hbox {
    -fx-background-color: red ;
}