java 如何从 fxml TextField 获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41683209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:03:04  来源:igfitidea点击:

How to get Text from fxml TextField

javajavafx

提问by Fomnus

I am trying to get the text from the firstName and lastName box from my .fxml file to my controller. I can't figure out how to get those firstName and lastName TextFields to pass the text through to the controller so i can then use them to create a person object

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

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
    }    

}

采纳答案by Bo Halim

First you set a controller to your FXML:

首先,您将控制器设置为FXML

fx:controller=<Controller.Location>

then in your cotroller you use the Annotation @FXML next to the Nodeto be initialized refering to its id in the FXML.file during the initialization :

然后在您Node的控制器中,您在初始化期间使用 FXML.file 中的 id旁边的注释 @FXML 进行初始化:

//Example :
@FXML TextField textfield;

and finally you set an ID to each Node you want to control (refering to the name you game to the Node:

最后你为你想要控制的每个节点设置一个 ID(指你游戏的名称Node

fx:id=<idFromController>

and to get the text :

并获取文本:

textfield.getText();