JavaFx文本字段值没有得到

时间:2016-02-01 12:00:21

标签: java javafx

我正在尝试使用JavaFx编写登录应用程序。 但我没有从文本字段和密码字段中获取值。

请参阅下面的代码。

以下函数用于设置阶段,其中有两个filds用户名和密码。

@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();

}

在这个函数中我调用另一个函数来验证用户名和密码。请参阅下面的代码。

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...");
           }
        }
    });
}

但是我无法在第一个函数中获得输入的用户名和密码。

1 个答案:

答案 0 :(得分:0)

问题在于

用户输入的值将在按钮句柄事件中可用。所以我修改了下面的代码,现在工作正常。

不是从下面的函数中获取用户名和密码值,而是将字段作为参数传递给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();

}

现在值在handle事件函数中。

这是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);
           }
        }
    });
}