Javafx验证用户输入用户名&密码

时间:2017-05-24 00:42:26

标签: java validation javafx input output

我有一个名为" AddUser"允许用户输入用户名和密码,这会将此信息添加到user.txt文件中。我还有一个名为" Login"获取用户输入的信息,用户名和密码,并根据user.txt文件验证输入。

但是,我无法弄清楚如何验证Login程序的输入。我在这里找到了其他几个帖子,但不是从文本文件中验证。任何帮助或指导都将非常感激。

添加用户

    import javax.swing.JOptionPane;
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.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_RIGHT);
        tfPassword.setAlignment(Pos.BOTTOM_RIGHT);

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

        // Process events
        btAddUser.setOnAction(e -> writeNewUser());

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

        // Create a scene and place it in the stage
        Scene scene = new Scene(gridPane, 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());
            bw.newLine();
            bw.write(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 javax.swing.JOptionPane;
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.stage.Stage;

import java.io.*;

public class Login extends Application {
    private TextField tfUsername = new TextField();
    private TextField tfPassword = new TextField();
    private Button btAddUser = new Button("Login");
    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_RIGHT);
        tfPassword.setAlignment(Pos.BOTTOM_RIGHT);

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

        // Process events
        btClear.setOnAction(e -> {
            tfUsername.clear();
            tfPassword.clear();
        });   

        // Create a scene and place it in the stage
        Scene scene = new Scene(gridPane, 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);
    }
}   

1 个答案:

答案 0 :(得分:1)

考虑这个示例评论中的说明):

// create boolean variable for final decision
boolean grantAccess = false;

// get the user name and password when user press on login button
// you already know how to use action listener 
// (i.e wrap the following code with action listener block of login button)
String userName = tfUsername.getText();
String password = tfPassword.getText();

File f = new File("users.txt");
try {
     Scanner read = new Scanner(f); 
     int noOfLines=0; // count how many lines in the file
     while(read.hasNextLine()){
           noOfLines++;
     }

    //loop through every line in the file and check against the user name & password (as I noticed you saved inputs in pairs of lines)
    for(int i=0; i<noOfLines; i++){
       if(read.nextLine().equals(userName)){ // if the same user name
          i++;
          if(read.nextLine().equals(password)){ // check password
             grantAccess=true; // if also same, change boolean to true
             break; // and break the for-loop
          }
       }
    }
     if(grantAccess){
        // let the user continue 
        // and do other stuff, for example: move to next window ..etc
     }
     else{
         // return Alert message to notify the deny
     }

} catch (FileNotFoundException e) {

        e.printStackTrace();
}