fxml设置背景,在FXML中以声明方式设置样式表

In HTML we are used to niceties of being able to set stylesheets programmatically like

But the examples of setting stylesheets I found for JavaFX require setting stylesheets programmatically, like

scene.getStylesheets().add("/resources/shell.css");

Is it possible to set stylesheets in FXML similar to the way it is done in HTML?

解决方案parent.getStylesheets().add("/resources/shell.css");

Because the elements and attributes usable in FXML are derived from the public JavaFX Java API, then you can also assign stylesheets to a Parent node using an FXML stylesheets element (or interchangeably an attribute). As all containers extend Parent, you can set one or more custom stylesheets on any container you reference in FXML:

....

styleClass="layout"

fx:controller="fruit.FruitComboController"

xmlns:fx="http://javafx.com/fxml">

....

See the fxml file in this sample for a complete executable sample of referencing a css file from an fxml file.

There are a couple of nice to have features in the above code:

The strange @ prefix in the stylesheets URL isn't strictly needed, but can be used to take advantage of JavaFX's location resolution. "The location resolution operator (represented by an "@" prefix to the attribute value) is used to specify that an attribute value should be treated as a location relative to the current file rather than a simple string."

The following line isn't required at runtime, but is used by the SceneBuilder tool to locate the required css stylesheet at design time if you are using that tool:

Update regarding the comment

Warning: It's FXML 1.0, it's not working in 2.0, javafx.fxml.LoadException: URL is not a valid type.

This comment is a bit incorrect I think. As far as I am aware, there is no such thing currently as FXML 2.0.

The reason the commenter received a LoadException was because the indicative snippet in this post was not importing the the java.net.URL class into the FXML document. I updated the snippet to include the java.net.URL import and add some more ellipsis .... to clarify the intent of the snippet. An ellipsis means "a series of dots that usually indicates an intentional omission of a word, sentence, or whole section from a text without altering its original meaning".

To best understand this answer it is suggested to compile and run the linked sample code.

Warning against using InputStream load functions in FXMLLoader

I strongly advise constructing an new FXMLLoader with a location, rather than using the FXMLLoader.load(InputStream) function. When the static load() function is used, then relative location references cannot be resolved as there is no base location for the FXML file.

I.e., don't do:

InputStream input = this.getClass().getResourceAsStream("layout.fxml");

FXMLLoader loader = new FXMLLoader.load(input);

Parent content = loader.load();

Instead do:

String url = this.getClass().getResource("layout.fxml").toExternalForm();

FXMLLoader loader = new FXMLLoader(url);

Parent content = loader.load();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值