如何在一行中设置 FXML "padding" 属性
How to set FXML "padding" property in one line
我想像在 C# XAML 文件中一样定义按钮的填充。而不是写这样的东西:
<Button fx:id="btn1" ...>
<padding>
<Insets top="0" right="5" bottom="10" left="5" />
</padding>
</Button>
<Button fx:id="btn2" ...>
<padding>
<Insets topRightBottomLeft="5" />
</padding>
</Button>
,这样写会更容易:
<Button fx:id="btn1" ... padding="0 5 10 5" />
<Button fx:id="btn2" ... padding="5" />
我不知道为什么 "padding" 属性 有这个限制。我在 FXML documentation 上看到 type coercion 可以实现 valueOf() 给定类型的方法:
public static Insets valueOf(String info) {
// Data retrieval
}
但是我不知道放在哪里,因为Insets class被锁定了(JDK)。因为这是一个静态方法,我认为该方法可以在其他地方实现,但是启动应用程序总是给我一个强制错误:
Caused by: java.lang.IllegalArgumentException: Unable to coerce 0 5 10 5 to class javafx.geometry.Insets.
目前,我找到的唯一解决方案是:
- 定义一个名为 GButton 的新 FXML 组件,它继承了 Button
- 定义一个继承Insets的Spacingclass并实现一个valueOf 这个 class
的方法
- 为 GButton 添加一个 "space" 属性(ObjectProperty
的实例),其行为与 "padding" 相同,并且定义它的 getter 和它的 setter.
是否有一种简单的方法可以将 FXML "padding" 定义到 Button 标记中,而无需创建从 Insets 继承的 class?
顺便说一句,如果问题有用,别忘了点赞:)
I don't know why is there this limitation for the "padding" property. I have seen on the FXML documentation that type coercion can be done implementing a valueOf()
method for the given type
这需要 Insets
实现 static
valueOf(String)
方法。那个类型不存在这个方法;因此您不能简单地通过属性指定值。
Additional conversions can be implemented by defining a static valueOf()
method on the target type.
您可以使用 style
属性 通过 CSS 指定值。这通常比将对象分配给 属性:
更短
<Button fx:id="btn1" style="-fx-padding: 0 5 10 5;" .../>
有关支持的 properties/syntax 的更多信息,请查看 the CSS Reference Guide。
我知道这个问题很老了,但希望我能提供一些有见地的信息。我有同样的问题;不幸的是,FXML 没有完整的文档,但在我看来,将初始化模型拼凑在一起使其变得直观。
无论如何,您的方法是正确的,只是没有完全完成:
<padding>
<Insets top="10" right="20" bottom="30" left="40"/>
</padding>
将创建一个等同于 CSS 的 padding: 10 20 30 40
的填充,当然,您可以更改它(甚至可以使用我假设的 FXML 变量)。
但是,一般来说,任何 class 在适当的约定中具有 javafx.beans
属性(请参阅 this tutorial 了解解释),您可以很容易地在 FXML 中创建自定义组件,并且像上面的 Insets
一样分配成员值。
希望对大家有所帮助!
我想像在 C# XAML 文件中一样定义按钮的填充。而不是写这样的东西:
<Button fx:id="btn1" ...>
<padding>
<Insets top="0" right="5" bottom="10" left="5" />
</padding>
</Button>
<Button fx:id="btn2" ...>
<padding>
<Insets topRightBottomLeft="5" />
</padding>
</Button>
,这样写会更容易:
<Button fx:id="btn1" ... padding="0 5 10 5" />
<Button fx:id="btn2" ... padding="5" />
我不知道为什么 "padding" 属性 有这个限制。我在 FXML documentation 上看到 type coercion 可以实现 valueOf() 给定类型的方法:
public static Insets valueOf(String info) {
// Data retrieval
}
但是我不知道放在哪里,因为Insets class被锁定了(JDK)。因为这是一个静态方法,我认为该方法可以在其他地方实现,但是启动应用程序总是给我一个强制错误:
Caused by: java.lang.IllegalArgumentException: Unable to coerce 0 5 10 5 to class javafx.geometry.Insets.
目前,我找到的唯一解决方案是:
- 定义一个名为 GButton 的新 FXML 组件,它继承了 Button
- 定义一个继承Insets的Spacingclass并实现一个valueOf 这个 class 的方法
- 为 GButton 添加一个 "space" 属性(ObjectProperty
的实例),其行为与 "padding" 相同,并且定义它的 getter 和它的 setter.
是否有一种简单的方法可以将 FXML "padding" 定义到 Button 标记中,而无需创建从 Insets 继承的 class?
顺便说一句,如果问题有用,别忘了点赞:)
I don't know why is there this limitation for the "padding" property. I have seen on the FXML documentation that type coercion can be done implementing a
valueOf()
method for the given type
这需要 Insets
实现 static
valueOf(String)
方法。那个类型不存在这个方法;因此您不能简单地通过属性指定值。
Additional conversions can be implemented by defining a
static valueOf()
method on the target type.
您可以使用 style
属性 通过 CSS 指定值。这通常比将对象分配给 属性:
<Button fx:id="btn1" style="-fx-padding: 0 5 10 5;" .../>
有关支持的 properties/syntax 的更多信息,请查看 the CSS Reference Guide。
我知道这个问题很老了,但希望我能提供一些有见地的信息。我有同样的问题;不幸的是,FXML 没有完整的文档,但在我看来,将初始化模型拼凑在一起使其变得直观。
无论如何,您的方法是正确的,只是没有完全完成:
<padding>
<Insets top="10" right="20" bottom="30" left="40"/>
</padding>
将创建一个等同于 CSS 的 padding: 10 20 30 40
的填充,当然,您可以更改它(甚至可以使用我假设的 FXML 变量)。
但是,一般来说,任何 class 在适当的约定中具有 javafx.beans
属性(请参阅 this tutorial 了解解释),您可以很容易地在 FXML 中创建自定义组件,并且像上面的 Insets
一样分配成员值。
希望对大家有所帮助!