Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.util.Scanner;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPasswordField;
  14. import javax.swing.JTextField;
  15.  
  16.  
  17. public class SarmientoFileAccess {
  18.     JFrame frame;
  19.     JLabel userLabel, passLabel;
  20.     JTextField userText;
  21.     JPasswordField passPF;
  22.     JButton loginButton;
  23.     Scanner scan;
  24.     int count;
  25.    
  26.     public static void main(String[] args) {
  27.         SarmientoFileAccess app = new SarmientoFileAccess();
  28.         app.initialize();
  29.     }
  30.    
  31.     public void loginButtonActionPerformed(ActionEvent evt) throws FileNotFoundException {
  32.         String username = userText.getText();
  33.         String password = passPF.getPassword().toString();
  34.         boolean loginSuccess = false;
  35.         scan = new Scanner(new File("UsernamesAndPasswords.txt"));
  36.        
  37.         while(scan.hasNext()) {
  38.             if(scan.nextLine().equals(username + " " + password)) {
  39.                 loginSuccess = true;
  40.                 break;
  41.             }
  42.         }
  43.        
  44.         if(loginSuccess) {
  45.             JOptionPane.showMessageDialog(frame, "Login Success! Previous failed login attempts: " + count, "Success!", JOptionPane.PLAIN_MESSAGE);
  46.         } else {
  47.             JOptionPane.showMessageDialog(frame, "Login Failed!", "Failed!", JOptionPane.ERROR_MESSAGE);
  48.             count++;
  49.         }
  50.        
  51.         //userText.setText(password);
  52.     }
  53.    
  54.     public void initialize() {
  55.         //initialize components
  56.         frame = new JFrame("Franz");
  57.         userLabel = new JLabel("Username:");
  58.         passLabel = new JLabel("Password:");
  59.         userText = new JTextField();
  60.         passPF = new JPasswordField();
  61.         loginButton = new JButton("Login");
  62.         count = 0;
  63.        
  64.         //set attributes
  65.         frame.setLocationRelativeTo(null);
  66.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.         frame.setPreferredSize(new Dimension(200, 120));
  68.         frame.setLayout(new FlowLayout());
  69.         frame.setResizable(false);
  70.         userText.setPreferredSize(new Dimension(100, 20));
  71.         passPF.setPreferredSize(new Dimension(100, 20));
  72.        
  73.         //add action listeners
  74.         loginButton.addActionListener(
  75.             new ActionListener() {
  76.                 public void actionPerformed(ActionEvent evt) {
  77.                     try {
  78.                         loginButtonActionPerformed(evt);
  79.                     } catch (FileNotFoundException e) {
  80.                         e.printStackTrace();
  81.                     }
  82.                 }
  83.             }
  84.         );
  85.        
  86.         //add components to frame
  87.         frame.getContentPane().add(userLabel);
  88.         frame.getContentPane().add(userText);
  89.         frame.getContentPane().add(passLabel);
  90.         frame.getContentPane().add(passPF);
  91.         frame.getContentPane().add(loginButton);
  92.        
  93.         //finalize GUI
  94.         frame.pack();
  95.         frame.setVisible(true);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement