gpt4 book ai didi

java fx : scene's lookup method

转载 作者:行者123 更新时间:2023-11-30 08:49:23 25 4
gpt4 key购买 nike

如果一个节点有 fx:id(比如 a)和正常的 id(css id)(比如 b)。如果使用 lookup 来搜索这个节点:

节点node=scene.lookup("#a")

以上语句返回null。但是如果讨论中没有对应节点的css id,那么上面的语句返回正确的节点。

虽然使用下面给定的语句总是返回正确的结果:

节点node=scene.lookup("#b")

谁能解释一下为什么查找方法的行为如此奇怪?

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<GridPane hgap="14.0" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="223.0" prefWidth="323.0" vgap="20.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" spacing="7.0" GridPane.rowIndex="0">
<children>
<HBox alignment="TOP_RIGHT">
<children>
<Label fx:id="headerMessage" text="message" textAlignment="LEFT" wrapText="true">
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="TOP_RIGHT" spacing="5.0">
<children>
<Label fx:id="qty" text="details" textAlignment="LEFT" wrapText="true">
<font>
<Font size="12.0" />
</font>
</Label>
<TextField fx:id="qtyT" />
</children>
</HBox>
<HBox alignment="TOP_RIGHT" spacing="5.0">
<children>
<Label fx:id="name" text="details" textAlignment="LEFT" wrapText="true">
<font>
<Font size="12.0" />
</font>
</Label>
<TextField fx:id="nameT" />
</children>
</HBox>
<HBox alignment="TOP_RIGHT" spacing="5.0">
<children>
<Label fx:id="remarks" text="details" textAlignment="LEFT" wrapText="true">
<font>
<Font size="12.0" />
</font>
</Label>
<TextArea fx:id="remarksT" prefHeight="60.0" prefWidth="149.0" wrapText="true" />
</children>
</HBox>
</children>
</VBox>
<HBox maxHeight="-Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="253.0" GridPane.rowIndex="1">
<children>
<Pane maxWidth="+Infinity" HBox.hgrow="ALWAYS" />
<Button id="cancel-button" fx:id="cancelButton" cancelButton="true" minWidth="80.0" mnemonicParsing="false" text="Cancel" HBox.hgrow="NEVER">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<HBox fx:id="okParent" alignment="CENTER">
<children>
<Button id="ok-button" fx:id="okButton" minWidth="80.0" mnemonicParsing="false" text="Ok" HBox.hgrow="NEVER">
<HBox.margin>
<Insets left="14.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</HBox>
</children>
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" maxWidth="+Infinity" minWidth="-Infinity" />
</columnConstraints>
<padding>
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0" />
</padding>
<rowConstraints>
<RowConstraints maxHeight="+Infinity" minHeight="-Infinity" valignment="CENTER" vgrow="ALWAYS" />
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" vgrow="NEVER" />
</rowConstraints>
</GridPane>

在上面的 fxml 中,对于没有任何 css id 的节点,查找在 fxml id 上工作正常。如果存在 css id,则返回空值

Label headerMessage = (Label) alertStage.getScene().lookup("#headerMessage");
final TextField qty = (TextField) alertStage.getScene().lookup("#qtyT");
Label name = (Label) alertStage.getScene().lookup("#name");
final TextArea remarksT = (TextArea) alertStage.getScene().lookup("#remarksT");
final TextField nameT = (TextField) alertStage.getScene().lookup("#nameT");

查找方法仅对下面给出的语句失败。

Button okButton = (Button) alertStage.getScene().lookup("#okButton");

最佳答案

编辑 - 在 OP 添加 MCVE 之后

深入研究这个问题。我在 FXML 文档的深处写了一个简单的声明,它解释了行为。实际上,通过调用 setId() 将 fx:id 指定为控件/布局的 默认 id

除非被覆盖,fx:id 保持为对象的默认 id

来自 Introduction To FXML :

Assigning an fx:id value to an element creates a variable in the document's namespace that can later be referred to by variable dereference attributes, such as the "toggleGroup" attribute shown above, or in script code, discussed in a later section. Additionally, if the object's type defines an "id" property, this value will also be passed to the objects setId() method.

lookup() 正在完美地完成它的工作,并且正在为 CSS 选择器 而不是 fx:id 工作。

关于java fx : scene's lookup method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471488/

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