Advertisement
reyhanzo

Untitled

Dec 15th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import javax.swing.*;
  2.  import java.awt.*;
  3.  import java.awt.event.*;
  4.  
  5.  public class Login{    
  6.      JTextField txtUsername = null;
  7.      JTextField txtPassword = null;
  8.      public static void main(String[] args){
  9.          Login gui = new Login();
  10.          gui.go();
  11.      }
  12.  
  13.      public void go(){
  14.          JFrame frame = new JFrame();
  15.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.          JPanel panel = new JPanel();
  17.          txtUsername = new JTextField(20);
  18.          txtPassword = new JPasswordField(20);
  19.  
  20.          JLabel lblUsername = new JLabel("Username:");  
  21.          JLabel lblPassword = new JLabel("Password:");
  22.        
  23.          JButton btnLogin = new JButton("Login");
  24.          btnLogin.addActionListener(new LoginListener(this));
  25.          JButton btnCancel = new JButton("Cancel");
  26.          btnCancel.addActionListener(new CancelListener(this));
  27.  
  28.          panel.add(lblUsername);
  29.          panel.add(txtUsername);
  30.          panel.add(lblPassword);
  31.          panel.add(txtPassword);
  32.        
  33.          panel.add(btnLogin);        
  34.          panel.add(btnCancel);
  35.          frame.getContentPane().add(BorderLayout.CENTER,panel);
  36.  
  37.          frame.setSize(300,300);
  38.          frame.setVisible(true);
  39.      }
  40.    
  41.      public void sendMessage(String msg) {
  42.          JOptionPane.showMessageDialog(null,msg);
  43.      }
  44.    
  45.      public String getUsername() {
  46.          return txtUsername.getText();
  47.      }
  48.  
  49.      public String getPassword() {
  50.          return txtPassword.getText();
  51.      }
  52.  
  53.      public void clear() {
  54.          txtUsername.setText("");
  55.          txtPassword.setText("");
  56.          txtUsername.requestFocus();
  57.      }
  58.    
  59.      public class LoginListener implements ActionListener{
  60.          String username = "reyhanzo";
  61.          String password = "rey123";
  62.          Login login;
  63.        
  64.          public LoginListener(Login login) {
  65.              this.login = login;
  66.          }
  67.        
  68.          public void actionPerformed(ActionEvent event){
  69.              if(username.equals(login.getUsername()) && password.equals(login.getPassword())){
  70.                  login.sendMessage("Welcome "+login.getUsername());
  71.              }else{
  72.                  login.sendMessage("Login Failed");
  73.              }  
  74.          }
  75.      }
  76.      public class CancelListener implements ActionListener{
  77.          Login login;
  78.        
  79.          public CancelListener(Login login) {
  80.              this.login = login;
  81.          }
  82.  
  83.          public void actionPerformed(ActionEvent event){
  84.              login.clear();
  85.          }
  86.      }
  87.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement