JavaFX - How to print text from a TextField

时间:2017-10-31 14:14:33

标签: java javafx scenebuilder

Currently learning JavaFX and its capabilities.

I used SceneBuilder to create a TextField and button. I want to print what the user typed in the TextField when the button is clicked.

Why does this not print the values that the user entered into the TextBox?

    public void generateButtonClicked() {

    TextField forehandWinner = new TextField();
    forehandWinner.getText();
    System.out.println(forehandWinner);  

FXML Button Code:

 <Button mnemonicParsing="false" onAction="#generateButtonClicked" text="Generate Statistics" />  

FXML TextField Code:

<TextField id="forehandWinner" fx:id="forehandWinner" alignment="CENTER" promptText="Forehand Winners">

1 个答案:

答案 0 :(得分:1)

  1. 在您的班级中,您必须声明FXML TextField对象:
  2. @FXML private TextField forehandWinner;

    1. 之后,您必须创建方法generateButtonClicked,如下所示:
    2. public void generateButtonClicked(ActionEvent event)

      1. 在generateButtonClicked方法中,执行以下操作:

        public void generateButtonClicked(ActionEvent event){ 的System.out.println(forehandWinner.getText()); }