Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- public class SarmientoFileAccess {
- JFrame frame;
- JLabel userLabel, passLabel;
- JTextField userText;
- JPasswordField passPF;
- JButton loginButton;
- Scanner scan;
- int count;
- public static void main(String[] args) {
- SarmientoFileAccess app = new SarmientoFileAccess();
- app.initialize();
- }
- public void loginButtonActionPerformed(ActionEvent evt) throws FileNotFoundException {
- String username = userText.getText();
- String password = passPF.getPassword().toString();
- boolean loginSuccess = false;
- scan = new Scanner(new File("UsernamesAndPasswords.txt"));
- while(scan.hasNext()) {
- if(scan.nextLine().equals(username + " " + password)) {
- loginSuccess = true;
- break;
- }
- }
- if(loginSuccess) {
- JOptionPane.showMessageDialog(frame, "Login Success! Previous failed login attempts: " + count, "Success!", JOptionPane.PLAIN_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(frame, "Login Failed!", "Failed!", JOptionPane.ERROR_MESSAGE);
- count++;
- }
- //userText.setText(password);
- }
- public void initialize() {
- //initialize components
- frame = new JFrame("Franz");
- userLabel = new JLabel("Username:");
- passLabel = new JLabel("Password:");
- userText = new JTextField();
- passPF = new JPasswordField();
- loginButton = new JButton("Login");
- count = 0;
- //set attributes
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setPreferredSize(new Dimension(200, 120));
- frame.setLayout(new FlowLayout());
- frame.setResizable(false);
- userText.setPreferredSize(new Dimension(100, 20));
- passPF.setPreferredSize(new Dimension(100, 20));
- //add action listeners
- loginButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- try {
- loginButtonActionPerformed(evt);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- );
- //add components to frame
- frame.getContentPane().add(userLabel);
- frame.getContentPane().add(userText);
- frame.getContentPane().add(passLabel);
- frame.getContentPane().add(passPF);
- frame.getContentPane().add(loginButton);
- //finalize GUI
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement