1 回答
TA贡献1793条经验 获得超6个赞
在 FXML 中,如果要定义可以在多个地方使用的单个变量,请使用fx:define. 从FXML 简介:
该<fx:define>元素用于创建存在于对象层次结构之外但可能需要在其他地方引用的对象。
例如,当使用单选按钮时,通常会定义一个ToggleGroup管理按钮选择状态的 。该组不是场景图本身的一部分,因此不应添加到按钮的父级。定义块可用于创建按钮组而不干扰文档的整体结构:
<VBox>
<fx:define>
<ToggleGroup fx:id="myToggleGroup"/>
</fx:define>
<children>
<RadioButton text="A" toggleGroup="$myToggleGroup"/>
<RadioButton text="B" toggleGroup="$myToggleGroup"/>
<RadioButton text="C" toggleGroup="$myToggleGroup"/>
</children>
</VBox>
定义块中的元素通常会被分配一个 ID,稍后可以使用该 ID 来引用元素的值。ID 将在后面的部分中更详细地讨论。
这是定义 a 半径的示例Circle:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.Double?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Circle?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" spacing="10.0" alignment="CENTER">
<padding>
<Insets topRightBottomLeft="10.0"/>
</padding>
<fx:define>
<Double fx:id="smallRadius" fx:value="50.0"/>
<Double fx:id="largeRadius" fx:value="100.0"/>
</fx:define>
<Circle radius="$smallRadius"/>
<Circle radius="$largeRadius"/>
<Circle radius="$smallRadius"/>
<Circle radius="$largeRadius"/>
</VBox>
添加回答
举报
