Advertisement
mhrabbi

Java Swing (WB) Login Window STest.01

Jan 20th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package loginSys;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JTextField;
  8. import javax.swing.JButton;
  9. import javax.swing.JLabel;
  10.  
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13.  
  14. import javax.swing.JPasswordField;
  15. import javax.swing.JSeparator;
  16.  
  17. import java.awt.Toolkit;
  18. import java.awt.Window.Type;
  19.  
  20. public class Login {
  21.  
  22.     private JFrame frame;
  23.     private JTextField txtUsername;
  24.     private JPasswordField txtPassword;
  25.  
  26.     /**
  27.      * Main Application
  28.      */
  29.     public static void main(String[] args) {
  30.         EventQueue.invokeLater(new Runnable() {
  31.             public void run() {
  32.                 try {
  33.                     Login window = new Login();
  34.                     window.frame.setVisible(true);
  35.                 } catch (Exception e) {
  36.                     e.printStackTrace();
  37.                 }
  38.             }
  39.         });
  40.     }
  41.  
  42.     /**
  43.      * Create the application.
  44.      */
  45.     public Login() {
  46.         initialize();
  47.     }
  48.  
  49.     /**
  50.      * Contents of the frame
  51.      */
  52.     private void initialize() {
  53.         frame = new JFrame();
  54.         frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Rabbi\\Pictures\\Untitled.png"));
  55.         frame.setBounds(100, 100, 450, 300);
  56.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.         frame.getContentPane().setLayout(null);
  58.        
  59.         txtUsername = new JTextField();
  60.         txtUsername.setBounds(172, 90, 123, 20);
  61.         frame.getContentPane().add(txtUsername);
  62.         txtUsername.setColumns(10);
  63.        
  64.         JButton btnLogin = new JButton("Login");
  65.         btnLogin.addActionListener(new ActionListener() {
  66.            
  67.             public void actionPerformed(ActionEvent arg0) {
  68.                
  69.                
  70.                 String password = txtPassword.getText();
  71.                 String username = txtUsername.getText();
  72.                
  73.                 if(password.contains("Hello")&&username.contains("Rabbi")){
  74.                    
  75.                     txtUsername.setText(null);
  76.                     txtPassword.setText(null);
  77.                 }
  78.                 else
  79.                 {
  80.                 JOptionPane.showMessageDialog(null,"Invalid Username Or Password", null,JOptionPane.ERROR_MESSAGE);
  81.             }
  82.                
  83.                
  84.                
  85.             }
  86.         });
  87.         btnLogin.setBounds(45, 203, 89, 23);
  88.         frame.getContentPane().add(btnLogin);
  89.        
  90.         JButton btnNewButton_1 = new JButton("Exit");
  91.         btnNewButton_1.addActionListener(new ActionListener() {
  92.             public void actionPerformed(ActionEvent arg0) {
  93.                 System.exit(0);
  94.             }
  95.         });
  96.         btnNewButton_1.setBounds(305, 203, 89, 23);
  97.         frame.getContentPane().add(btnNewButton_1);
  98.        
  99.         JLabel lblLoginSystem = new JLabel("Login System");
  100.         lblLoginSystem.setBounds(187, 11, 95, 43);
  101.         frame.getContentPane().add(lblLoginSystem);
  102.        
  103.         JLabel lblUserName = new JLabel("UserName");
  104.         lblUserName.setBounds(85, 93, 65, 14);
  105.         frame.getContentPane().add(lblUserName);
  106.        
  107.         JLabel lblPassword = new JLabel("Password");
  108.         lblPassword.setBounds(85, 134, 68, 14);
  109.         frame.getContentPane().add(lblPassword);
  110.        
  111.         txtPassword = new JPasswordField();
  112.         txtPassword.setBounds(172, 131, 123, 20);
  113.         frame.getContentPane().add(txtPassword);
  114.        
  115.         JSeparator separator = new JSeparator();
  116.         separator.setBounds(131, 52, 186, 2);
  117.         frame.getContentPane().add(separator);
  118.        
  119.         JButton btnReset = new JButton("Reset");
  120.         btnReset.addActionListener(new ActionListener() {
  121.             public void actionPerformed(ActionEvent arg0) {
  122.                 txtUsername.setText(null);
  123.                 txtPassword.setText(null);
  124.            
  125.             }
  126.         });
  127.         btnReset.setBounds(172, 203, 89, 23);
  128.         frame.getContentPane().add(btnReset);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement