【发布时间】:2015-07-17 08:32:00
【问题描述】:
如果一个节点具有 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 中,在 fxml id 上查找没有任何 css id 的节点可以正常工作。如果存在 css id,则返回 nulls
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");
lookup 方法仅对下面给出的语句失败。
Button okButton = (Button) alertStage.getScene().lookup("#okButton");
【问题讨论】:
-
这很奇怪。我确信
fx:id,它应该总是返回null。你能添加一个描述这种行为的MCVE吗?
标签: java css user-interface javafx-2 javafx-8