gpt4 book ai didi

javafx - 使用 FXML 在用户控件中创建并绑定(bind) DoubleProperty

转载 作者:行者123 更新时间:2023-12-02 06:45:34 26 4
gpt4 key购买 nike

我创建了一个代表数字键盘的用户控件。我希望将各个按钮的宽度和高度实现为 DoubleProperty,以便稍后进行配置。

我遇到的问题是我无法将该属性绑定(bind)到 GridPane 列和行的 prefWidthprefHeight FXML 文件。我设法做到这一点的唯一方法是在控制 Controller 中建立连接。但如果可能的话我想避免它。

这是有问题的 FXML 文件。

<fx:root type="javafx.scene.layout.GridPane" xmlns="http://javafx.com/javafx/" xmlns:fx="http://javafx.com/fxml/1">
<fx:define>
<SimpleDoubleProperty fx:id="size">
<value>
<Double fx:value="50"/>
</value>
</SimpleDoubleProperty>
</fx:define>

<gridLinesVisible>false</gridLinesVisible>

<hgap>5</hgap>
<vgap>5</vgap>

<Button text="C" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="0" focusTraversable="false"/>
<Button text="Backspace" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="0" GridPane.columnSpan="2" focusTraversable="false"/>

<Button text="7" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="1" focusTraversable="false"/>
<Button text="8" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1" focusTraversable="false"/>
<Button text="9" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="1" focusTraversable="false"/>

<Button text="4" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="2" focusTraversable="false"/>
<Button text="5" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="2" focusTraversable="false"/>
<Button text="6" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="2" focusTraversable="false"/>

<Button text="1" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="3" focusTraversable="false"/>
<Button text="2" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="3" focusTraversable="false"/>
<Button text="3" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="3" focusTraversable="false"/>

<Button text="0" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="4" GridPane.columnSpan="2" focusTraversable="false"/>
<Button text="." maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="4" focusTraversable="false"/>

<Button text="E" onAction="#f" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="3" GridPane.rowIndex="2" GridPane.rowSpan="3" focusTraversable="false"/>

<columnConstraints>
<ColumnConstraints fx:id="col1" prefWidth="${size.value}"/>
<ColumnConstraints fx:id="col2" prefWidth="${size.value}"/>
<ColumnConstraints fx:id="col3" prefWidth="${size.value}"/>
<ColumnConstraints fx:id="col4" prefWidth="${size.value}"/>
</columnConstraints>
<rowConstraints>
<RowConstraints fx:id="row1" prefHeight="${size.value}"/>
<RowConstraints fx:id="row2" prefHeight="${size.value}"/>
<RowConstraints fx:id="row3" prefHeight="${size.value}"/>
<RowConstraints fx:id="row4" prefHeight="${size.value}"/>
<RowConstraints fx:id="row5" prefHeight="${size.value}"/>
</rowConstraints>
</fx:root>

${size.value} 正确初始化控件,但 sizeColumnConstraints#prefWidthProperty/ 之间没有绑定(bind)RowConstraints#prefHeightProperty

如果我手动进行绑定(bind),一切正常。但正如我所说,如果可能的话,我想避免它。

@FXML
private void initialize() {
col1.prefWidthProperty().bind(size);
// ...

row1.prefHeightProperty().bind(size);
// ...
}

如果我使用 ${size} 我得到 NumberFormatException: For input string: "DoubleProperty [value: 50.0]"

最佳答案

通过表达式绑定(bind)直接访问属性似乎是不可能的。

但是,如果您通过 Controller 提供对该属性的访问,则可以使用 ${controller.size} 来创建绑定(bind):

@FXML
private DoubleProperty size;

public final double getSize() {
return size.get();
}

public final void setSize(double value) {
size.set(value);
}

public final DoubleProperty sizeProperty() {
return size;
}
...
<columnConstraints>
<ColumnConstraints fx:id="col1" prefWidth="${controller.size}"/>
<ColumnConstraints fx:id="col2" prefWidth="${controller.size}"/>
<ColumnConstraints fx:id="col3" prefWidth="${controller.size}"/>
<ColumnConstraints fx:id="col4" prefWidth="${controller.size}"/>
</columnConstraints>
<rowConstraints>
<RowConstraints fx:id="row1" prefHeight="${controller.size}"/>
<RowConstraints fx:id="row2" prefHeight="${controller.size}"/>
<RowConstraints fx:id="row3" prefHeight="${controller.size}"/>
<RowConstraints fx:id="row4" prefHeight="${controller.size}"/>
<RowConstraints fx:id="row5" prefHeight="${controller.size}"/>
</rowConstraints>
...

关于javafx - 使用 FXML 在用户控件中创建并绑定(bind) DoubleProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52754803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com