我正在尝试按照 Oracle 提供的教程自学基本的 JavaFX。
在 BorderPane 教程 ( https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm ) 中,它指定背景颜色。
这是我的代码片段:
/**
* 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 文件,但它仅向“消息”添加样式。
最佳答案
原始代码的唯一问题是您的样式设置中有一个“拼写错误”(英语化?)。应该是
hBox.setStyle("-fx-background-color: #FFFFFF;");
不是
hBox.setStyle("-fx-background-colour: #FFFFFF;");
使用外部样式表
#hbox {
-fx-background-color: red ;
}
是比使用内联样式更好的解决方案。
关于JavaFX BorderPane 将不会采用背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950398/