JavaFX:如何给多个圆形相同的大小?

我的问题看起来很简单,但我还没有找到好的方法。我想在我的 FXML 中定义几个圆,10 个圆的半径应为 10px,其他 20 个圆应具有 6px 之一。我想一次性声明大小,以便我可以轻松更改它。

我没有发现是否/如何在 FXML 中声明变量,这将是最简单的方法。所以我希望 CSS 能帮助我,这会更好:我试图给圆圈两个 styleClasses 并在 CSS 中设置它们的大小,但似乎没有缩放圆圈的属性。

<Circle fx:id="c00" centerX="100" centerY="100" styleClass="circle10" />

另一个想法是覆盖 Circle 类并将半径设置为所需的值。但我不认为这是一个干净的代码,因为布局应该在 FXML 和 CSS 中进行,而不是在 Java 中。

必须有一种方法可以做到这一点,而不会在那个地方弄乱 Java 代码。

在此先感谢您的帮助!迷雾剂


心有法竹
浏览 158回答 1
1回答

摇曳的蔷薇

在 FXML 中,如果要定义可以在多个地方使用的单个变量,请使用fx:define. 从FXML 简介:该<fx:define>元素用于创建存在于对象层次结构之外但可能需要在其他地方引用的对象。例如,当使用单选按钮时,通常会定义一个ToggleGroup管理按钮选择状态的 。该组不是场景图本身的一部分,因此不应添加到按钮的父级。定义块可用于创建按钮组而不干扰文档的整体结构:<VBox>&nbsp; &nbsp; <fx:define>&nbsp; &nbsp; &nbsp; &nbsp; <ToggleGroup fx:id="myToggleGroup"/>&nbsp; &nbsp; </fx:define>&nbsp; &nbsp; <children>&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton text="A" toggleGroup="$myToggleGroup"/>&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton text="B" toggleGroup="$myToggleGroup"/>&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton text="C" toggleGroup="$myToggleGroup"/>&nbsp; &nbsp; </children>&nbsp;</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">&nbsp; &nbsp; <padding>&nbsp; &nbsp; &nbsp; &nbsp; <Insets topRightBottomLeft="10.0"/>&nbsp; &nbsp; </padding>&nbsp; &nbsp; <fx:define>&nbsp; &nbsp; &nbsp; &nbsp; <Double fx:id="smallRadius" fx:value="50.0"/>&nbsp; &nbsp; &nbsp; &nbsp; <Double fx:id="largeRadius" fx:value="100.0"/>&nbsp; &nbsp; </fx:define>&nbsp; &nbsp; <Circle radius="$smallRadius"/>&nbsp; &nbsp; <Circle radius="$largeRadius"/>&nbsp; &nbsp; <Circle radius="$smallRadius"/>&nbsp; &nbsp; <Circle radius="$largeRadius"/></VBox>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java