[英]JavaFx text field value not getting
我正在嘗試使用 JavaFx 編寫登錄應用程序。 但是我沒有從文本字段和密碼字段中獲取值。
請看下面我的代碼。
以下函數用於設置階段,其中有兩個字段用戶名和密碼。
@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...");
}
}
});
}
但是我無法在它自己的第一個函數中獲得輸入的用戶名和密碼。
問題是
用戶輸入的值將在按鈕句柄事件中可用。 所以我修改了下面的代碼,現在工作正常。
這些字段不是從下面的函數中獲取用戶名和密碼值,而是作為參數傳遞給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);
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.