StackPane layoutY="70.0"
prefHeight="479.0"
。我希望在Java文件中将值(70.0)
和(479.0)
设为静态,以便在其他文件中使用。
这种做法是否可行?
如果您的常量在一个类中被定义:
public class SomeClass {
public static final double DEFAULT_HEIGHT = 479 ;
// ...
}
然后您可以在FXML中按如下方式访问它:
<StackPane>
<prefHeight>
<SomeClass fx:constant="DEFAULT_HEIGHT" />
</prefHeight>
</StackPane>
确保您在使用的fxml文件中为所使用的类添加了适当的导入。
James_D展示了使用自定义类的方式。在fxml中另一种方法是定义自己的变量,但它们不能在文件之间共享。
不要这样做
<StackPane layoutY="70.0" prefHeight="479.0">
你希望拥有
<StackPane layoutY="$variable" prefHeight="$variable">
你可以像这样做
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" id="AnchorPane" prefHeight="200" prefWidth="320" fx:controller="javafxapplication22.FXMLDocumentController">
<fx:define>
<Double fx:id="layoutY" fx:value="70.0"/>
<Double fx:id="prefHeight" fx:value="479.0"/>
</fx:define>
<children>
<StackPane layoutY="$layoutY" prefHeight="$prefHeight"/>
<Pane layoutY="$layoutY" prefHeight="$prefHeight"/>
</children>
</AnchorPane>