创建按钮时JavaFX Scene失去颜色 [英] JavaFX Scene loses color when a button is created
问题描述
有人能解释为什么我在JavaFX中创建按钮后场景会失去颜色吗?
Can anyone explain why my scene loses color the moment I create a button in JavaFX?
下面的代码有效,场景的背景变为红色
The following code works, with the background of the scene changing to red
@Override
public void start(Stage primaryStage){
//Set Primary stage title and create a rootNode
primaryStage.setTitle("Hello World");
FlowPane rootNode = new FlowPane();
//Create a scene and add it to the rootNode
Scene myScene = new Scene(rootNode, 300, 200, Color.RED);
//Add the scene to the stage
primaryStage.setScene(myScene);
//Show the stage
primaryStage.show();
}
但是,当我创建另一个控件时,就像下面示例中的按钮一样(而且我什至不必将其添加到流窗格中),颜色会恢复为灰色。
However, the moment I create another control, like a button in the example below (and I don't even have to add it to the flowpane), the color reverts back to grey.
@Override
public void start(Stage primaryStage){
//Set Primary stage title and create a rootNode
primaryStage.setTitle("Hello World");
FlowPane rootNode = new FlowPane();
//Create a scene and add it to the rootNode
Scene myScene = new Scene(rootNode, 300, 200, Color.CORAL);
Button newBtn = new Button();
//Add the scene to the stage
primaryStage.setScene(myScene);
//Show the stage
primaryStage.show();
}
有人知道这是为什么吗?我是否要错误地更改背景颜色?
Anyone know why this is? Am I trying to change the background color incorrectly?
推荐答案
您的场景背景颜色根本不可见,因为 rootNode
涵盖了整个场景,而 rootNode
具有自己的背景色,该背景色在默认JavaFx主题中设置(即您的灰色
Your scene background color should not be visible at all, because rootNode
covers the whole scene, and rootNode
has its own background color which is set in the default JavaFx theme (that's the grey color you're seeing):
//modena.css
.root {
...
/***************************************************************************
* *
* Set the default background color for the scene *
* *
**************************************************************************/
-fx-background-color: -fx-background;
}
因此,您需要更改的背景颜色rootNode
,如已经提出的其他答案。
So, you need to change the background color of rootNode
, as the other answer already suggested.
剩下的问题是为什么在第一个示例中未应用默认的根背景色到 rootNode
(它是透明的,而不应该是),您会看到场景的背景色。
The remaining question is why, in your first example, the default root background color is not applied to rootNode
(it's transparent and it shouldn't be) and you see the scene's background color instead.
答案-可能是一个错误。在JavaFx中,默认主题是通过 PlatformImpl.setDefaultPlatformUserAgentStylesheet()
方法设置的,仅在以下情况下会调用:
The answer - it's probably a bug. In JavaFx, the default theme is set with the method PlatformImpl.setDefaultPlatformUserAgentStylesheet()
which is called only in these cases:
- when you call
Application.setUserAgentStylesheet
(source) - in static initializer block of
Control
andPopupControl
classes (source and source)
FlowPane
都不扩展 Control
或 PopupControl
,因此JavaFx甚至不会加载默认主题,并且您的 rootNode
保持透明(您可以
FlowPane
extends neither Control
nor PopupControl
, so JavaFx doesn't even load the default theme and your rootNode
remains transparent (you see the scene's background color).
在另一个示例中,您创建了一个 Button
控件,该控件扩展了 Control
类,因此将执行 Control
类的静态初始化程序块并加载默认的Modena主题-您的 rootPane
从默认主题获得其默认的灰色,并且您不再看到场景的背景色。
In your other example you create a Button
control, which extends Control
class, so static initializer block of Control
class is executed and the default modena theme is loaded - your rootPane
gets its default grey color from the default theme and you no longer see the scene's background color.
这篇关于创建按钮时JavaFX Scene失去颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!