按下FXML按钮后从Textfield获取文本

时间:2018-05-01 23:30:00

标签: java scenebuilder

按下按钮后,我试图将文本放在文本字段中。

程序运行正常,fxml加载并且程序正常工作但是每当我按下提交按钮时,就会打印一个空行而不是文本字段中的内容。

这是Java和FXML代码:

Java文件:

package me.Excursion.UI;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class Login extends Application {

    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
            Scene scene = new Scene(root, 1200, 800);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    @FXML
    private void loginSubmitClick(ActionEvent e){
        TextField Username = new TextField();
        System.out.println(Username.getText());
    }
}

FXML文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.Excursion.UI.Login">
   <children>
      <ImageView fitHeight="822.0" fitWidth="1200.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@pictures/Blurred Sea.jpg" />
         </image>
      </ImageView>
      <AnchorPane layoutX="50.0" layoutY="40.0" prefHeight="720.0" prefWidth="520.0" style="-fx-opacity: 0.75; -fx-background-color: white;" />
      <AnchorPane layoutX="84.0" layoutY="72.0" prefHeight="656.0" prefWidth="450.0">
         <children>
            <TextField fx:id="Username" layoutX="102.0" layoutY="255.0" prefHeight="26.0" prefWidth="248.0" promptText="Username" />
            <PasswordField fx:id="Password" layoutX="101.0" layoutY="339.0" prefHeight="26.0" prefWidth="248.0" promptText="Password" />
            <ImageView fitHeight="208.0" fitWidth="226.0" layoutX="121.0" layoutY="30.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@pictures/54330b43-40d1-419a-a4b7-e04805c54560.png" />
               </image>
            </ImageView>
            <Button layoutX="145.0" layoutY="438.0" mnemonicParsing="false" onAction="#loginSubmitClick" prefHeight="40.0" prefWidth="160.0" style="-fx-background-color: #083B66;" text="Submit" />
            <Hyperlink layoutX="166.0" layoutY="505.0" style="-fx-underline: true;" text="Forgot Password?" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:1)

而不是创建新的TextField,声明一个类变量

@FXML
private TextField Username;

这将起作用