gpt4 book ai didi

JavaFX:仅声明节点即可更改其他节点的背景

转载 作者:行者123 更新时间:2023-11-30 06:10:39 25 4
gpt4 key购买 nike

我有一个程序,它首先启动一个小闪屏,5 秒后它继续到一个新的阶段。

奇怪的是,到目前为止它工作得很好,但是现在我开始构建新的阶段,我在类中声明新的节点来使用它。

现在,启动画面突然变成白色背景,注释掉之前创建的节点将使图像恢复为透明。

public static Pane pane = new Pane(); //this is fine

//Uncommenting this Node will change the splash screen background white
//public static TextArea textArea = new TextArea();

public static Image splashImage = new Image(Class.class.getClassLoader().getResourceAsStream("image.png"));
public static ImageView splashImageView = new ImageView(splashImage);
public static BorderPane splashPane = new BorderPane(splashImageView);
public static Scene splashScene = new Scene(splashPane);

@Override
public void start(Stage primaryStage) throws Exception {

splashScene.setFill(Color.TRANSPARENT);

primaryStage.setScene(splashScene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();

}

这是我的代码的缩短版本

这可能是因为内存问题还是其他什么原因?我从来没有遇到过这样的事情。

感谢任何帮助。

最佳答案

仅当创建控件(即 Control 的实例或其子类之一)时,才会加载默认样式表。 (其背后的想法是避免为管理所有自己的图形且不使用任何控件(例如游戏或模拟)的应用程序加载 CSS 的性能开销。)

默认样式表将根节点(示例中的 splashPane)的背景颜色设置为非常浅的灰色(具体来说,它比颜色 #ececec< 亮 26.4%)/)。

由于文本区域是类中唯一的控件,因此创建它会导致加载默认样式表,从而将 splashPane 的背景颜色设置为非常浅的灰色。

如果您需要实例化控件并希望 Pane 的背景透明,则需要在外部 CSS 中指定:

splashStyle.css:

.root {
-fx-background-color: transparent ;
}

还有

@Override
public void start(Stage primaryStage) throws Exception {

splashScene.setFill(Color.TRANSPARENT);
splashScene.getStylesheets().add("splashStyle.css");

primaryStage.setScene(splashScene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();

}

(为了快速检查这是否有效,您可以使用

进行测试
splashPane.setStyle("-fx-background-color: transparent; ");

)

关于JavaFX:仅声明节点即可更改其他节点的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com