在fxml中定义自定义类[关闭](Define your custom class in fxml [closed])

编程入门 行业动态 更新时间:2024-10-10 01:25:04
在fxml中定义自定义类[关闭](Define your custom class in fxml [closed])

我有一个名为“语言”的自定义类,我想在fxml中定义它,如:

<Language fx:value="getBundle"> </Language>

语言直接扩展Object而不是任何JavaFX组件。 显然它抛出以下异常:

无法强制com.bl.commons.Language@69435到类javafx.scene.Node

那我该怎么做? 我想要的是,我的大部分模态都在该类中,我想直接调用Language方法并在fxml本身中设置内容。 如果这不可能那么任何替代方案?

I have a custom class called 'Language', I want to define it in fxml like:

<Language fx:value="getBundle"> </Language>

Language extends Object directly and not any JavaFX component. Obviously it throws the below Exception:

Unable to coerce com.bl.commons.Language@69435 to class javafx.scene.Node

So how do I do it? What I want is that, most of my modal is in that class and I want to directly call methods of Language and set content in fxml itself. If this is not possible then any alternatives?

最满意答案

据我所知,你的类必须以某种方式扩展Node才能被添加到其他元素的子列表中(当你把它放在FXML的任何地方时你正在做的事情),因为那些子Node存储为ObservableList<Node> 。

请注意,这并不意味着你必须做任何特别的事情,只需这样做:

public class Language extends Node // or StackPane or whatever { public Language() { super(); // ...carry on... } }

如果Language已经扩展了其他内容,那么您将希望该类扩展Node或重新考虑您的类层次结构。

As far as I know your class has to extend Node in some way in order to be added to the list of children of other elements (which is what you are doing when you put it anywhere in FXML), because those children are stored as ObservableList<Node>.

Note that this doesn't really mean you have to do anything special, just do:

public class Language extends Node // or StackPane or whatever { public Language() { super(); // ...carry on... } }

If Language already extends something else you will want to make that class extend Node or rethink your class hierarchy.

更多推荐

本文发布于:2023-08-07 17:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   定义   fxml   Define   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!