JavaFX-为整个应用程序设置默认的CSS样式表 [英] JavaFX - Set default CSS stylesheet for the whole Application
问题描述
我对JavaFX还是很陌生,我正在尝试通过CSS更改应用程序方面.第一次工作正常,当我切换到另一个场景时,CSS的类未应用.
因为我的项目有10个场景,所以我更喜欢使用一个CSS文件通过以下语句将样式应用于所有场景:
StyleManager.getInstance().addUserAgentStylesheet(this.getClass().getResource("/style.css").toExternalForm());
进入start()
方法.
如何在全局范围内将我的CSS文件应用于所有场景?
您可以执行以下两行操作:
Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA);
StyleManager.getInstance().addUserAgentStylesheet(getClass().getResource("/style.css").toString());
第一行将使用
into start()
method.
How can I apply to all scenes my CSS file globally?
You can do it with the following two lines:
Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA);
StyleManager.getInstance().addUserAgentStylesheet(getClass().getResource("/style.css").toString());
The first line will set the default stylesheet to modena (here you could even choose to set JavaFX2 caspian as default stylesheet) using Application#setUserAgentStylesheet
, while the second line will add your stylesheet to the user agent stylesheet list.
It's not the nicest solution as StlyeManager
is a private API (and will remain in JDK9 as it is now).
Other solution would be to subclass Scene
, in which subclass you can add your stylesheet by default, then rather than instantiating Scene
, you could create instances of your subclass.
这篇关于JavaFX-为整个应用程序设置默认的CSS样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!