Advertisement
arnobkumarsaha

Login

Apr 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1.  
  2. package LoginApproval;
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. public class LoginFrame extends JFrame implements ActionListener {
  10.  
  11.     Container container = getContentPane();
  12.     JLabel userLabel = new JLabel("USERNAME");
  13.     JLabel passwordLabel = new JLabel("PASSWORD");
  14.     JTextField userTextField = new JTextField();
  15.     JPasswordField passwordField = new JPasswordField();
  16.     JButton loginButton = new JButton("LOGIN");
  17.     JButton resetButton = new JButton("RESET");
  18.     JCheckBox showPassword = new JCheckBox("Show Password");
  19.  
  20.  
  21.     LoginFrame() {
  22.         setLayoutManager();
  23.         setLocationAndSize();
  24.         addComponentsToContainer();
  25.         addActionEvent();
  26.  
  27.     }
  28.  
  29.     public void setLayoutManager() {
  30.         container.setLayout(null);
  31.     }
  32.  
  33.     public void setLocationAndSize() {
  34.         userLabel.setBounds(50, 150, 100, 30);
  35.         passwordLabel.setBounds(50, 220, 100, 30);
  36.         userTextField.setBounds(150, 150, 150, 30);
  37.         passwordField.setBounds(150, 220, 150, 30);
  38.         showPassword.setBounds(150, 250, 150, 30);
  39.         loginButton.setBounds(50, 300, 100, 30);
  40.         resetButton.setBounds(200, 300, 100, 30);
  41.  
  42.  
  43.     }
  44.  
  45.     public void addComponentsToContainer() {
  46.         container.add(userLabel);
  47.         container.add(passwordLabel);
  48.         container.add(userTextField);
  49.         container.add(passwordField);
  50.         container.add(showPassword);
  51.         container.add(loginButton);
  52.         container.add(resetButton);
  53.     }
  54.  
  55.     public void addActionEvent() {
  56.         loginButton.addActionListener(this);
  57.         resetButton.addActionListener(this);
  58.         showPassword.addActionListener(this);
  59.     }
  60.  
  61.  
  62.     @Override
  63.     public void actionPerformed(ActionEvent e) {
  64.         //Coding Part of LOGIN button
  65.         if (e.getSource() == loginButton) {
  66.             String userText;
  67.             String pwdText;
  68.             userText = userTextField.getText();
  69.             pwdText = passwordField.getText();
  70.             if (userText.equalsIgnoreCase("arnob") && pwdText.equalsIgnoreCase("12345")) {
  71.                 JOptionPane.showMessageDialog(this, "Login Successful");
  72.             } else {
  73.                 JOptionPane.showMessageDialog(this, "Invalid Username or Password");
  74.             }
  75.  
  76.         }
  77.         //Coding Part of RESET button
  78.         if (e.getSource() == resetButton) {
  79.             userTextField.setText("");
  80.             passwordField.setText("");
  81.         }
  82.        //Coding Part of showPassword JCheckBox
  83.         if (e.getSource() == showPassword) {
  84.             if (showPassword.isSelected()) {
  85.                 passwordField.setEchoChar((char) 0);
  86.             } else {
  87.                 passwordField.setEchoChar('*');
  88.             }
  89.  
  90.  
  91.         }
  92.     }
  93.  
  94. }
  95.  
  96.  
  97.  
  98. package LoginApproval;
  99.  
  100. import javax.swing.JFrame;
  101.  
  102. public class Login {
  103.     public static void main(String[] a) {
  104.         LoginFrame frame = new LoginFrame();
  105.         frame.setTitle("Login Form");
  106.         frame.setVisible(true);
  107.         frame.setBounds(10, 10, 370, 600);
  108.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109.         frame.setResizable(false);
  110.  
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement