Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package areatrainer;
- import static areatrainer.AreaTrainer.CurrentUser;
- import static areatrainer.AreaTrainer.login;
- import javafx.event.ActionEvent;
- import java.io.IOException;
- import java.net.URL;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.List;
- import java.util.ResourceBundle;
- import javafx.application.Application;
- import static javafx.application.Application.launch;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.fxml.Initializable;
- import javafx.scene.Node;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Stage;
- /**
- * FXML Controller class
- *
- * @author joel
- */
- public class Login extends Application implements Initializable {
- @FXML
- TextField usernameTxt;
- @FXML
- TextField passwordTxt;
- @FXML
- Button loginBttn;
- @FXML
- public static void main(String[] args) {
- launch(args);
- }
- @FXML
- @Override
- public void start(Stage stage) throws IOException {
- Parent root = FXMLLoader.load(getClass().getResource("loginScreen.fxml"));
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.setTitle("Login");
- stage.show();
- }
- @FXML
- public void login(ActionEvent event) {
- try {
- String username = usernameTxt.getText();
- String password = passwordTxt.getText();
- List<String> usernames = Files.readAllLines(Paths.get("usernames.txt"));
- List<String> passwords = Files.readAllLines(Paths.get("passwords.txt"));
- for (int i = 0; i < usernames.size(); i++) {
- if (usernames.get(i).equals(username) && passwords.get(i).equals(password)) {
- CurrentUser = username;
- Alert alert = new Alert(AlertType.INFORMATION);
- alert.setTitle("Login Success!");
- alert.setHeaderText(null);
- alert.setContentText("Welcome, " + CurrentUser);
- alert.showAndWait();
- }
- }
- if (CurrentUser.equals("")) {
- Alert incorrectUser = new Alert(AlertType.ERROR);
- incorrectUser.setTitle("Login Failure");
- incorrectUser.setHeaderText(null);
- incorrectUser.setContentText("Incorrect Username or Password!");
- incorrectUser.showAndWait();
- }
- } catch (IOException e) {
- }
- }
- @FXML
- public void register(ActionEvent event) {
- try {
- Parent root1 = FXMLLoader.load(getClass().getResource("register.fxml"));
- Stage stage = new Stage();
- stage.setScene(new Scene(root1));
- stage.setTitle("Register");
- stage.show();
- }catch (IOException e) {}
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- // TODO
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement