gpt4 book ai didi

java - 如何从 fxml TextField 获取文本

转载 作者:行者123 更新时间:2023-11-30 10:34:22 27 4
gpt4 key购买 nike

我正在尝试从我的 .fxml 文件中的 firstName 和 lastName 框中获取文本到我的 Controller 。我不知道如何让这些 firstName 和 lastName TextFields 将文本传递给 Controller ​​,这样我就可以使用它们创建一个人对象

fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>

<AnchorPane id="AnchorPane" fx:id="createPersonAnchor" prefHeight="247.0" prefWidth="285.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button layoutX="171.0" layoutY="180.0" mnemonicParsing="false" onAction="#cancelCreatePersonAction" prefHeight="37.0" prefWidth="79.0" text="Cancel" />
<TextField id="FirstName" fx:id="firstName" layoutX="115.0" layoutY="28.0" />
<TextField id="LastName" fx:id="lastName" layoutX="115.0" layoutY="78.0" />
<Button layoutX="47.0" layoutY="180.0" mnemonicParsing="false" onAction="#createPersonAction" prefHeight="37.0" prefWidth="79.0" text="Create" />
<Text layoutX="29.0" layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="First Name:" wrappingWidth="69.677734375" />
<Text layoutX="29.0" layoutY="95.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Last Name:" wrappingWidth="69.677734375" />
</children>
</AnchorPane>

Controller :

package project1;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
* FXML Controller class
*
* @author Fomnus
*/
public class FXMLCreatePersonController implements Initializable {

/**
* Initializes the controller class.
*/
public FXMLCreatePersonController(){
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLCreatePerson.fxml"));
loader.setController(this);
try{
loader.load();
}catch(Exception e){
System.out.println(e.getMessage()+ "FXMLCreatePersonController failed");
}
}
@FXML AnchorPane createPersonAnchor;
@FXML public void setFXMLCreatePersonAnchorPane(AnchorPane fxmlCreatePerson){
createPersonAnchor = fxmlCreatePerson;
}
@FXML public AnchorPane getFXMLCreatePersonAnchorPane(){
return createPersonAnchor;
}
@FXML
private void createPersonAction(ActionEvent event) {

}
@FXML
private void cancelCreatePersonAction(ActionEvent event) {
System.out.println("Hello World");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

最佳答案

首先你为你的FXML设置了一个 Controller :

fx:controller=<Controller.Location>

然后在您的 Controller 中,您使用要初始化的 Node 旁边的注解 @FXML,在初始化期间引用其在 FXML.file 中的 id:

//Example :
@FXML TextField textfield;

最后你为每个你想控制的节点设置一个ID(指的是你给Node游戏的名字:

fx:id=<idFromController>

并获取文本:

textfield.getText();

关于java - 如何从 fxml TextField 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41683209/

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