简体   繁体   English

JavaFx 文本字段值未获取

[英]JavaFx text field value not getting

I am trying to write a login application using JavaFx.我正在尝试使用 JavaFx 编写登录应用程序。 But I am not getting the value from the text filed and password field.但是我没有从文本字段和密码字段中获取值。

Please see below my code.请看下面我的代码。

The following function is used to set the stage and in it there are two filds username and password.以下函数用于设置阶段,其中有两个字段用户名和密码。

@Override
public void start(Stage primaryStage) {

    //setStage(primaryStage);


    primaryStage.setTitle("JavaFX Welcome");
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));

    Text scenetitle = new Text("Welcome");
    scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(scenetitle, 0, 0, 2, 1);

    Label userName = new Label("User Name:");
    grid.add(userName, 0, 1);

    TextField userTextField = new TextField();
    grid.add(userTextField, 1, 1);

    Label pw = new Label("Password:");
    grid.add(pw, 0, 2);

    PasswordField pwBox = new PasswordField();
    grid.add(pwBox, 1, 2);

    Button btn = new Button("Sign in");
    HBox hbBtn = new HBox(10);
    hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
    hbBtn.getChildren().add(btn);
    grid.add(hbBtn, 1, 4);

    final Text actiontarget = new Text();
    grid.add(actiontarget, 1, 6);

    String username = userTextField.getText();
    String password = pwBox.getText();
    logger.info("from start---userame:"+username+"::pswd:"+password);
    handleEvent(btn,actiontarget,username,password);


    Scene scene = new Scene(grid, 300, 275);
    primaryStage.setScene(scene);


    primaryStage.show();

}

With in this function I am calling another function to validate the username and password.在这个函数中,我调用另一个函数来验证用户名和密码。 See code below.请参阅下面的代码。

public void handleEvent(Button btn,Text actiontarget,String username,String password)
{

    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {
            actiontarget.setFill(Color.FIREBRICK);

           boolean result = validateuser(username,password);

           if(result){
              actiontarget.setText("Login Success");
           }
           else{
               actiontarget.setText("Login Fail...");
           }
        }
    });
}

But I am not been able to get the entered username and password in the first function it self.但是我无法在它自己的第一个函数中获得输入的用户名和密码。

The problem is that问题是

the user entered values will be available in the button handle event.用户输入的值将在按钮句柄事件中可用。 So I modified the code as below and is working fine now.所以我修改了下面的代码,现在工作正常。

Instead of getting the username and password value from the below function, the fields are passed as an argument to the handlEvent() function这些字段不是从下面的函数中获取用户名和密码值,而是作为参数传递给handlEvent() 函数

@Override
public void start(Stage primaryStage) {

    primaryStage.setTitle(LoginConstants.FX_WELCOME);
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));

    Text scenetitle = new Text(LoginConstants.TEXT_WELCOME);
    scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(scenetitle, 0, 0, 2, 1);

    Label userName = new Label(LoginConstants.LABEL_USERNAME);
    grid.add(userName, 0, 1);

    TextField userTextField = new TextField();
    grid.add(userTextField, 1, 1);

    Label pw = new Label(LoginConstants.LABEL_PASSWORD);
    grid.add(pw, 0, 2);

    PasswordField pwBox = new PasswordField();
    grid.add(pwBox, 1, 2);

    Button btn = new Button(LoginConstants.BUTTON_SUBMIT);
    HBox hbBtn = new HBox(10);
    hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
    hbBtn.getChildren().add(btn);
    grid.add(hbBtn, 1, 4);

    final Text actiontarget = new Text();
    grid.add(actiontarget, 1, 6);

    handleEvent(btn,actiontarget,userTextField,pwBox);

    Scene scene = new Scene(grid, 300, 275);
    primaryStage.setScene(scene);

    primaryStage.show();

}

Now the values are taken inside the handle event function.现在这些值是在 handle 事件函数中获取的。

Here is the handleEvent()这是 handleEvent()

public void handleEvent(Button btn,Text actiontarget,TextField userTextField,PasswordField pwBox)
{

    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {

            String username = userTextField.getText();
            String password = pwBox.getText();

            logger.info("from start---userame:"+username+"::pswd:"+password);

            actiontarget.setFill(Color.FIREBRICK);

           boolean result = validateuser(username,password);

           if(result){
              actiontarget.setText(LoginConstants.TEXT_LOGIN_SUCCESS);
           }
           else{
               actiontarget.setText(LoginConstants.TEXT_LOGIN_FAIL);
           }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM