Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package areatrainer;
  7.  
  8. import static areatrainer.AreaTrainer.CurrentUser;
  9. import static areatrainer.AreaTrainer.login;
  10. import javafx.event.ActionEvent;
  11. import java.io.IOException;
  12. import java.net.URL;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.util.List;
  16. import java.util.ResourceBundle;
  17. import javafx.application.Application;
  18. import static javafx.application.Application.launch;
  19. import javafx.fxml.FXML;
  20. import javafx.fxml.FXMLLoader;
  21. import javafx.fxml.Initializable;
  22. import javafx.scene.Node;
  23. import javafx.scene.Parent;
  24. import javafx.scene.Scene;
  25. import javafx.scene.control.Alert;
  26. import javafx.scene.control.Alert.AlertType;
  27. import javafx.scene.control.Button;
  28. import javafx.scene.control.TextField;
  29. import javafx.scene.layout.StackPane;
  30. import javafx.stage.Stage;
  31.  
  32. /**
  33.  * FXML Controller class
  34.  *
  35.  * @author joel
  36.  */
  37. public class Login extends Application implements Initializable {
  38.  
  39.     @FXML
  40.     TextField usernameTxt;
  41.     @FXML
  42.     TextField passwordTxt;
  43.     @FXML
  44.     Button loginBttn;
  45.  
  46.     @FXML
  47.  
  48.     public static void main(String[] args) {
  49.         launch(args);
  50.     }
  51.  
  52.     @FXML
  53.  
  54.     @Override
  55.     public void start(Stage stage) throws IOException {
  56.         Parent root = FXMLLoader.load(getClass().getResource("loginScreen.fxml"));
  57.         Scene scene = new Scene(root);
  58.         stage.setScene(scene);
  59.         stage.setTitle("Login");
  60.         stage.show();
  61.  
  62.     }
  63.  
  64.     @FXML
  65.     public void login(ActionEvent event) {
  66.  
  67.         try {
  68.             String username = usernameTxt.getText();
  69.             String password = passwordTxt.getText();
  70.             List<String> usernames = Files.readAllLines(Paths.get("usernames.txt"));
  71.             List<String> passwords = Files.readAllLines(Paths.get("passwords.txt"));
  72.  
  73.             for (int i = 0; i < usernames.size(); i++) {
  74.                 if (usernames.get(i).equals(username) && passwords.get(i).equals(password)) {
  75.                     CurrentUser = username;
  76.                     Alert alert = new Alert(AlertType.INFORMATION);
  77.                     alert.setTitle("Login Success!");
  78.                     alert.setHeaderText(null);
  79.                     alert.setContentText("Welcome, " + CurrentUser);
  80.                     alert.showAndWait();
  81.                 }
  82.             }
  83.             if (CurrentUser.equals("")) {
  84.                 Alert incorrectUser = new Alert(AlertType.ERROR);
  85.                 incorrectUser.setTitle("Login Failure");
  86.                 incorrectUser.setHeaderText(null);
  87.                 incorrectUser.setContentText("Incorrect Username or Password!");
  88.                 incorrectUser.showAndWait();
  89.             }
  90.  
  91.         } catch (IOException e) {
  92.         }
  93.  
  94.     }
  95.  
  96.     @FXML
  97.     public void register(ActionEvent event) {
  98.    
  99.            
  100.         try {
  101.             Parent root1 = FXMLLoader.load(getClass().getResource("register.fxml"));
  102.             Stage stage = new Stage();
  103.             stage.setScene(new Scene(root1));
  104.             stage.setTitle("Register");
  105.             stage.show();
  106.          
  107.        
  108.         }catch (IOException e) {}
  109.  
  110.      
  111.     }
  112.  
  113.     @Override
  114.     public void initialize(URL url, ResourceBundle rb) {
  115.         // TODO
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement