JavaFX 控制器未初始化 UI 字段
JavaFX Controller not getting UI fields initialized
我正在通过 Scene Builder 开发 JavaFX 项目。
我创建了一个相当长的 FXML 文件(我只报告了一个片段)和相关的控制器。此外,我写了我的申请 class:
public class Main extends Application {
@Override
public void start(Stage stage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("myfxml.fxml"));
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene scene = new Scene(root);
stage.setTitle("Popolamento dati Ambasciata");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1150.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FXController">
...
<children>
<Button id="addobject" mnemonicParsing="false" onAction="#addobject" text="Add Object" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="10" />
<TextField id="objectname" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1" />
控制器:
public class FXController {
@FXML Button addobject;
@FXML TextField objectname;
@FXML
public void addobject() {
objectname.getText();
}
}
}
应用程序正确启动并调用了事件处理程序,但在访问TextField objectname 时,它为空。
我哪里做错了?
您应该使用 fx:id
属性而不是 id
。
如果你查看文档 here:
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
请注意,您仍然可以使用 id
属性:
Additionally, if the object's type defines an "id" property, this value will also be passed to the objects setId() method.
所以在你的情况下:
<Button fx:id="addobject" ... />
<TextField fx:id="objectname" ... />
我正在通过 Scene Builder 开发 JavaFX 项目。 我创建了一个相当长的 FXML 文件(我只报告了一个片段)和相关的控制器。此外,我写了我的申请 class:
public class Main extends Application {
@Override
public void start(Stage stage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("myfxml.fxml"));
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene scene = new Scene(root);
stage.setTitle("Popolamento dati Ambasciata");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1150.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FXController">
...
<children>
<Button id="addobject" mnemonicParsing="false" onAction="#addobject" text="Add Object" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="10" />
<TextField id="objectname" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1" />
控制器:
public class FXController {
@FXML Button addobject;
@FXML TextField objectname;
@FXML
public void addobject() {
objectname.getText();
}
}
}
应用程序正确启动并调用了事件处理程序,但在访问TextField objectname 时,它为空。 我哪里做错了?
您应该使用 fx:id
属性而不是 id
。
如果你查看文档 here:
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
请注意,您仍然可以使用 id
属性:
Additionally, if the object's type defines an "id" property, this value will also be passed to the objects setId() method.
所以在你的情况下:
<Button fx:id="addobject" ... />
<TextField fx:id="objectname" ... />