createDefaultSkin()在JavaFX自定义控件中返回null错误 [英] createDefaultSkin() returned null error in JavaFX custom control

查看:209
本文介绍了createDefaultSkin()在JavaFX自定义控件中返回null错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的Stackoverflow社区,

Dear Stackoverflow community,

我在JavaFX中实现了一个自定义控件,扩展了 Control 类。
一切正常,但我一直收到错误消息

I've implemented a custom control in JavaFX extending Control class. Everything is just working fine, but I keep getting the error message

Jan 30, 2015 8:33:31 AM javafx.scene.control.Control impl_processCSS
SEVERE: The -fx-skin property has not been defined in CSS for 
CustomView@19e4d42 and createDefaultSkin() returned null.

我读了很多关于 SkinBase BehaviorBase 的内容,但由于一切正常,我只是想如果可能的话,如果没有实现这些类,这个错误就会消失。

I read a lot about SkinBase and BehaviorBase, but since everything is working as it is supposed to, I would just like this error to disappear without implementing these classes if possible.

我没有使用CSS,我找不到任何手动设置默认皮肤的方法。

I'm not using CSS and I can't find any method to manually set a default skin.

我真的很感谢你的帮助。在此先感谢!

I would really appreciate your help. Thanks in advance!

推荐答案

自JavaFX 8(Java SE 8附带)以来,方法 createDefaultSkin() -method,你们可以(应该?)覆盖自定义控件。

Since JavaFX 8 (comes with Java SE 8), there is the method createDefaultSkin()-method, which you can (should?) override in a custom control.

此外,我建议在自定义控件的构造函数中调用以下语句:

Furthermore I recommend to call the following statement in the constructor of your custom control:

getStyleClass().setAll("my-custom-control");

然后重写方法 getUserAgentStylesheet()并在引用的css文件中添加:

Then override the method getUserAgentStylesheet() and in the referenced css file add:

.my-custom-control {
    -fx-skin: "mypackage.impl.skin.MyCustomControlSkin";
}

此引用类是自定义控件的默认外观。

where this reference class is the default skin of your custom control.

但正如文档和错误消息所解释的那样,也可以覆盖上述方法或仅提供css。

But as the documentation and error message explains, it's also ok to just override the mentioned method or just to provide the css.

如果你不需要skinnable控件但只想创建一个由其他控件组成的自定义控件,那么我建议写一个 fx:基于root的控件而不是扩展Control。另请查看我的以下答案:我们是否应该在JavaFX自定义控件中使用FXML?

If you don't need a skinnable control but just want to create a custom control which is composed of other controls then I suggest to write a fx:root based control instead of extending Control. Also have a look at my following answer: Should we use FXML in JavaFX custom controls or not?

这篇关于createDefaultSkin()在JavaFX自定义控件中返回null错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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