首页 > 解决方案 > 用于用户验证的 javafx 程序。添加用户程序效果很好。登录程序有运行时错误。尝试了很多解决方案,需要帮助

问题描述

让“登录”程序正常运行时遇到问题。AddUser 效果很好,登录没有,最终没有响应。我已经尝试了对“登录”的许多代码更改,并且感觉我几乎拥有它,但我得到的最好的结果是显示“无效的用户名或密码”错误的 GUI,尽管两者都是正确的。需要帮助请!

这是 AddUser.java 的代码,它将选定的用户名和密码写入 users.txt 文件:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.*;

public class AddUser extends Application {
private TextField tfUsername = new TextField();
private TextField tfPassword = new TextField();
private Button btAddUser = new Button("Add User");
private Button btClear = new Button("Clear");

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
    // Create UI
    GridPane gridPane = new GridPane();
    gridPane.setHgap(5);
    gridPane.setVgap(5);
    gridPane.add(new Label("Username:"), 0, 0);
    gridPane.add(tfUsername, 1, 0);
    gridPane.add(new Label("Password:"), 0, 1);
    gridPane.add(tfPassword, 1, 1);
    gridPane.add(btAddUser, 1, 3);
    gridPane.add(btClear, 1, 3);

    // Set properties for UI
    gridPane.setAlignment(Pos.CENTER);
    tfUsername.setAlignment(Pos.BOTTOM_LEFT);
    tfPassword.setAlignment(Pos.BOTTOM_LEFT);

    GridPane.setHalignment(btAddUser, HPos.RIGHT);
    GridPane.setHalignment(btClear, HPos.LEFT);

    Text text = new Text(" ");
    text.setFont(Font.font("Copperplate Gothic Light", FontWeight.SEMI_BOLD, FontPosture.REGULAR, 16));
    text.setFill(Color.BLUE);


    // Process events
    btAddUser.setOnAction(e -> {
    writeNewUser();
    text.setText("User " + tfUsername.getText()+ " Added!");
    });


    btClear.setOnAction(e -> {
        tfUsername.clear();
        tfPassword.clear();
        text.setText("  ");
    });

    VBox vbox = new VBox(2);
    vbox.getChildren().addAll(text, gridPane);
    // Create a scene and place it in the stage
    Scene scene = new Scene(vbox, 300, 150);
    primaryStage.setTitle("Add User"); // Set title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
}

public void writeNewUser() {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("users.txt", true))) {
        bw.write(tfUsername.getText()+"|"+tfPassword.getText());
        bw.newLine();

    }
    catch (IOException e){
        e.printStackTrace();
    }
}

/**
 * The main method is only needed for the IDE with limited
 * JavaFX support. Not needed for running from the command line.
 */
public static void main(String[] args) {
    launch(args);
}
}

现在,这是“登录”程序的代码,这就是我遇到麻烦的地方。

    import java.io.*;
    import java.util.*;

    import javafx.application.Application;
    import javafx.geometry.HPos;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontPosture;
    import javafx.scene.text.FontWeight;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;

    public class Login extends Application {
     private TextField tfUser = new TextField();
     private PasswordField tfPass = new PasswordField();
     private Button btLogin = new Button("Login");
     private Button btClear = new Button("Clear");


@SuppressWarnings("resource")
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
    // Create UI
    GridPane gridPane = new GridPane();
    gridPane.setHgap(5);
    gridPane.setVgap(5);
    gridPane.add(new Label("Username:"), 0, 0);
    gridPane.add(tfUser, 1, 0);
    gridPane.add(new Label("Password:"), 0, 1);
    gridPane.add(tfPass, 1, 1);
    gridPane.add(btLogin, 1, 3);
    gridPane.add(btClear, 1, 3);

    // Set properties for UI
    gridPane.setAlignment(Pos.CENTER);
    tfUser.setAlignment(Pos.BOTTOM_LEFT);
    tfPass.setAlignment(Pos.BOTTOM_LEFT);

    GridPane.setHalignment(btLogin, HPos.RIGHT);
    GridPane.setHalignment(btClear, HPos.LEFT);

    Text text = new Text();
    text.setFont(Font.font("Copperplate Gothic Light", FontWeight.SEMI_BOLD, FontPosture.REGULAR, 16));


    // Process events
    btClear.setOnAction(e ->
   {
        tfUser.clear();
        tfPass.clear();
        text.setText("");
    });


    btLogin.setOnAction(e -> {
        boolean grantAccess = false;
        String userName = tfUser.getText();
        String password = tfPass.getText();

        File f = new File("users.txt");
        try {
        Scanner read = new Scanner(f);
        int noOfLines=0;
        while(read.hasNextLine()) {
           noOfLines++;

        }

        for(int i=0; i<noOfLines; i++) {
            if (read.nextLine().equals(userName)) {
                i++;
                if (read.nextLine().equals(password)) {
                 grantAccess=true;
                 break;
                }
            }
        }

    if(grantAccess) {
        text.setText("Welcome!");
        text.setFill(Color.BLUEVIOLET);
    }
    else {text.setText("Invalid Username or Password!");
    text.setFill(Color.RED);
    }
    }
     catch(FileNotFoundException e1) {
        e1.printStackTrace();
    }
});

    VBox vbox = new VBox(2);
    vbox.getChildren().addAll(text, gridPane);

      // Create a scene and place it in the stage
    Scene scene = new Scene(vbox, 300, 150);
    primaryStage.setTitle("LOGIN"); // Set title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
}


/**
 * The main method is only needed for the IDE with limited
 * JavaFX support. Not needed for running from the command line.
 */
public static void main(String[] args) {
    launch(args);
}
}

任何帮助是极大的赞赏!!!!谢谢!

标签: javafxtext-files

解决方案


推荐阅读