JavaFX:仅声明节点更改其他节点的背景 [英] JavaFX: Just Declaring Nodes changes Background of other Nodes

查看:67
本文介绍了JavaFX:仅声明节点更改其他节点的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序首先启动一个小启动屏幕,然后在5秒钟后继续进入新的舞台.

I have a program that first starts up a little splash screen, and after 5 seconds it continues to a new Stage.

奇怪的是,直到现在为止一切正常,但是现在我开始构建新的Stage,我在类中声明了要使用它的新Node.

The strange thing is, until now it worked fine, but now that I'm starting to build the new Stage I'm declaring new Nodes in the class to use it.

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

Now all of the sudden the splash screen has a white background, commenting the before created nodes out will revert the image back to being transparent.

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();

}

这是我的代码的简化版本

Here a shortened version of my code

这可能是由于内存问题或其他原因吗?我从未遇到过这样的事情.

Could this be because of memory issues or something? I have never encountered anything like this.

感谢您的帮助.

推荐答案

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

The default style sheet is only loaded if a control (i.e an instance of Control or one of its subclasses) is created. (The idea behind this is to avoid the performance overhead of loading CSS for applications that manage all their own graphics and don't use any controls, such as games or simulations.)

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

The default stylesheet sets the background color of the root node (the splashPane in your example) to a very light grey (specifically, it is 26.4% brighter than the color #ececec).

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

Since the text area is the only control in your class, creating it causes the default style sheet to be loaded, which sets the background color of the splashPane to a very light grey.

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

If you need to instantiate controls and want the background of the pane to be transparent, you need to specify that in an external 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();

}

(为快速检查其是否可行,您可以使用

(As a quick check that this will work, you can just test with

splashPane.setStyle("-fx-background-color: transparent; ");

)

这篇关于JavaFX:仅声明节点更改其他节点的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆