Advertisement
Guest User

Untitled

a guest
May 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7.  
  8. public class Login extends JFrame {
  9.     JPanel Panel;
  10.    
  11.     JLabel txtUser;
  12.     JLabel txtPw;
  13.     JLabel status;
  14.    
  15.     JTextField user;
  16.     JPasswordField pw;
  17.    
  18.     JButton submit;
  19.    
  20.     String[][] users = {
  21.         {"admin","1234"},
  22.         {"unknown","666"}
  23.     };
  24.     private String user_name;
  25.     private String user_pw;
  26.    
  27.     public Login() {
  28.         setSize(290,150);
  29.         setLocation(100,100);
  30.         setTitle("Login");
  31.        
  32.         Panel = new JPanel();
  33.         Panel.setLayout(null);
  34.         Panel.setBackground(Color.decode("#C16262"));
  35.        
  36.         txtUser = new JLabel("User:");
  37.         txtPw = new JLabel("Password:");
  38.         status = new JLabel("");
  39.         txtUser.setBounds(20,13,80,20);
  40.         txtPw.setBounds(20,47,80,20);
  41.         status.setBounds(20,110,300,20);
  42.         status.setForeground(Color.pink);
  43.        
  44.         user = new JTextField(10);
  45.         pw = new JPasswordField(10);
  46.         user.setBounds(110,15,150,20);
  47.         pw.setBounds(110,50,150,20);
  48.        
  49.         submit = new JButton("Submit");
  50.         submit.setBounds(110,85,100,20);
  51.  
  52.         Panel.add(txtUser);
  53.         Panel.add(txtPw);
  54.         Panel.add(status);
  55.         Panel.add(user);
  56.         Panel.add(pw);
  57.         Panel.add(submit);
  58.        
  59.         getContentPane().add(Panel);
  60.        
  61.         setVisible(true);
  62.        
  63.         submit.addActionListener(new ActionListener() {
  64.             public void actionPerformed(ActionEvent e) {
  65.                 user_name = user.getText().toLowerCase();
  66.                 user_pw = String.valueOf(pw.getPassword());
  67.  
  68.                 if (user_name.length() > 3 && user_pw.length() > 2){
  69.                     boolean done = false;
  70.                     for(int i = 0;i<users.length;i++){
  71.                         if (user_name.equals(users[i][0]) && user_pw.equals(users[i][1])){
  72.                             done = true;
  73.                             break;
  74.                         }
  75.                     }
  76.                    
  77.                     if (done){
  78.                         new Bank();
  79.                         status.setText(null);
  80.                     }else{
  81.                         status.setText("Invalid Username / Password!");
  82.                         pw.setText(null);
  83.                     }
  84.                 }else{
  85.                     status.setText("Enter an existent Login / Password!");
  86.                     user.setText(null);
  87.                     pw.setText(null);
  88.                 }                              
  89.             }
  90.         });
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement