我是java的新手,下面为动作监听器编写的onButtonClick()方法始终在else块中执行该语句。我需要它来验证用户名和密码并执行if-else块。奇怪的是,当我在每种方法中添加了一些println时,我注意到控件根本没有解析为main。我启动程序后,将首先执行start方法中的print语句,而不是main中的第一个print语句。而且main中的两个print语句都根本没有执行。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.Button;

    public class Version3 extends Application {
        public static void main(String args) {
            System.out.println("main called"); //this is not executing
            launch(args);
            System.out.println("Finished"); // this aswell
    }
    Button signInButton;
    Button cancelButton;
    Text userNameText;
    Text passwordText;
    Text Validation;
    TextField userNameField;
    PasswordField passwordField;
    //Using gridpane and intialized all Fields above

    public void start(Stage primaryStage) {
        System.out.println("Stage called");
   //added a statement to check when this method was calling. Statement
   //getting executed as soon as the program started.

        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setVgap(20);
        pane.setHgap(20);

        userNameText = new Text("Username");
        pane.add(userNameText, 0, 0);

        userNameField = new TextField();
        pane.add(userNameField, 1, 0);

        passwordText = new Text("Password");
        pane.add(passwordText, 0, 1);

        passwordField = new PasswordField();
        pane.add(passwordField, 1, 1);

        signInButton = new Button("Submit");
        pane.add(signInButton, 0, 2);

        signInButton.setOnAction(e -> onButtonClick());
        // calling method for signin button
        Validation = onButtonClick();
        pane.add(Validation, 0, 3);

        cancelButton = new Button("Clear");
        pane.add(cancelButton, 1, 2);

        Scene scene = new Scene(pane, 900, 600);

        primaryStage.setScene(scene);
        primaryStage.setTitle("The Click me app");
        primaryStage.show();

    }

    public Text onButtonClick() {
       System.out.println("onButton called");
       if (userNameField.getText().equals("admin") &&
       passwordField.getText().equals("password")) {
       Text val = new Text("Credentials Validated");
       //this is neither getting validated nor executing
       return val;
       } else {
       Text val = new Text("Invalid Credentials");
       // Always displaying this else block on layout
       return val;
       }
   }
}

最佳答案

首先,您的main方法应如下所示

public static void main(String[] args){
    System.out.println("main called");
    launch(args);
    System.out.println("Finished");
}

其次,更改start(Stage primaryStage)方法中的语句


Validation = onButtonClick();




Validation = new Text("Invalid Credentials");

第三,将onButtonClick()方法更改为

public void onButtonClick(){
    System.out.println("onButton called");
    if(userNameField.getText().equals("admin") && passwordField.getText().equals("password")){
      Validation.setText("Credentials Validated");
    }else{
      Validation.setText("Invalid Credentials");
    }
}

10-01 20:24
查看更多