Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.File;
  7. import java.util.Scanner;
  8. import javax.swing.*;
  9.  
  10.  
  11.     public class LoginGUI extends JFrame {
  12.  
  13.  
  14.         private JLabel loginLabel;
  15.         private JLabel passwordLabel;
  16.         private JPasswordField passwordTextField;
  17.         private JLabel usernameLabel;
  18.         private JTextField usernameTextField;
  19.         private JButton loginButton;
  20.         private Scanner x;
  21.         public LoginGUI(){
  22.             setGUI();
  23.             actions();
  24.         }
  25.         private void setGUI(){
  26.  
  27.             this.setTitle("Login");
  28.             this.setSize(500,250);
  29.  
  30.             JPanel contentPane = new JPanel(null);
  31.             contentPane.setPreferredSize(new Dimension(500,250));
  32.             contentPane.setBackground(new Color(0,204,204));
  33.  
  34.  
  35.             loginLabel = new JLabel();
  36.             loginLabel.setBounds(180,5,166,69);
  37.             loginLabel.setBackground(new Color(255,255,255));
  38.             loginLabel.setForeground(new Color(255,255,255));
  39.             loginLabel.setEnabled(true);
  40.             loginLabel.setFont(new Font("monaco",0,40));
  41.             loginLabel.setText("Login");
  42.             loginLabel.setVisible(true);
  43.  
  44.             passwordLabel = new JLabel();
  45.             passwordLabel.setBounds(10,135,90,35);
  46.             passwordLabel.setBackground(new Color(214,217,223));
  47.             passwordLabel.setForeground(new Color(255,255,255));
  48.             passwordLabel.setEnabled(true);
  49.             passwordLabel.setFont(new Font("monaco",0,15));
  50.             passwordLabel.setText("Password");
  51.             passwordLabel.setVisible(true);
  52.  
  53.             passwordTextField = new JPasswordField();
  54.             passwordTextField.setBounds(163,135,290,35);
  55.             passwordTextField.setBackground(new Color(255,255,255));
  56.             passwordTextField.setForeground(new Color(0,0,0));
  57.             passwordTextField.setEnabled(true);
  58.             passwordTextField.setFont(new Font("sansserif",0,12));
  59.             passwordTextField.setVisible(true);
  60.  
  61.             loginButton = new JButton();
  62.             loginButton.setBounds(150,190,200,45);
  63.             loginButton.setBackground(new Color(153,255,153));
  64.             loginButton.setForeground(new Color(153,255,153));
  65.             loginButton.setEnabled(true);
  66.             loginButton.setFont(new Font("monaco",0,12));
  67.             loginButton.setText("Login");
  68.             loginButton.setVisible(true);
  69.  
  70.             usernameLabel = new JLabel();
  71.             usernameLabel.setBounds(10,88,90,35);
  72.             usernameLabel.setBackground(new Color(214,217,223));
  73.             usernameLabel.setForeground(new Color(255,255,255));
  74.             usernameLabel.setEnabled(true);
  75.             usernameLabel.setFont(new Font("monaco",0,15));
  76.             usernameLabel.setText("Username");
  77.             usernameLabel.setVisible(true);
  78.  
  79.             usernameTextField = new JTextField();
  80.             usernameTextField.setBounds(163,87,290,35);
  81.             usernameTextField.setBackground(new Color(255,255,255));
  82.             usernameTextField.setForeground(new Color(0,0,0));
  83.             usernameTextField.setEnabled(true);
  84.             usernameTextField.setFont(new Font("sansserif",0,12));
  85.             usernameTextField.setVisible(true);
  86.  
  87.  
  88.             contentPane.add(loginLabel);
  89.             contentPane.add(passwordLabel);
  90.             contentPane.add(passwordTextField);
  91.             contentPane.add(usernameLabel);
  92.             contentPane.add(usernameTextField);
  93.             contentPane.add(loginButton);
  94.  
  95.  
  96.             this.add(contentPane);
  97.             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  98.             this.setLocationRelativeTo(null);
  99.             this.pack();
  100.             this.setVisible(true);
  101.         }
  102.  
  103.         private void actions(){
  104.             loginButton.addActionListener(new ActionListener() {
  105.                 @Override
  106.                 public void actionPerformed(ActionEvent e) {
  107.                     String username = usernameTextField.getText();
  108.                     String password = passwordTextField.getText();
  109.                     String fileName = "members.rtf";
  110.                     authentication(username,password,fileName);
  111.                 }
  112.             });
  113.         }
  114.  
  115.         public void authentication(String username, String password, String fileName){
  116.             boolean found = false;
  117.             String tempUsername = "";
  118.             String tempPassword = "";
  119.             try{
  120.                 x = new Scanner(new File(fileName));
  121.                 x.useDelimiter("[,\n]");
  122.  
  123.                 while(x.hasNext() && !found){
  124.                     tempUsername = x.next();
  125.                     tempPassword = x.next();
  126.                     if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())){
  127.                         found = true;
  128.                     }
  129.                 }
  130.                 x.close();
  131.                 System.out.println(found);
  132.             }catch(Exception e){
  133.                 System.out.println("error");
  134.             }
  135.         }
  136.  
  137.  
  138.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement