Monday, 3 November 2014

Write a java program to validate user login and password. If they do not match, display appropriate message in a dialog box. The user is allowed maximum 3 chances. (Use the screen designed in Assignment 6a1.)





Name : Shreyal Mandot

Title : Assignment 7a2. Write a java program to validate user login and password. If they do not match, display appropriate message in a dialog box. The user is allowed maximum 3 chances. (Use the screen designed in Assignment 6a1.)




import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ass7a2 extends JFrame implements ActionListener
{

private JTextField t1;
private JPasswordField t2;
private JButton b1,b2;
private JLabel l1,l2;

int n;

    public ass7a2(String s)
    {
 
    super(s);
     
        n=0;
             
        t1=new JTextField(10);
        t2=new JPasswordField(10);
     
        b1=new JButton("OK");
        b1.addActionListener(this);
     
        b2=new JButton("Cancel");
        b2.addActionListener(this);
             
        l1=new JLabel("Login Name");
        l2=new JLabel("Password");
             
        t2.setEchoChar('*');
     
        setLayout(new GridLayout(3,2));
        add(l1);add(t1);
        add(l2);add(t2);
        add(b1);add(b2);
     
}

    public void actionPerformed(ActionEvent e)
    {
             
    if(e.getSource()==b1)
        {
     
        String s=t1.getText();
         
            char c[]=new char[50];
            c=t2.getPassword();
                     
            String s1=new String(c);
                     
            if(s.equals("admin") && s1.equals("admin"))
            {
         
            JOptionPane.showMessageDialog(null,"Success");
                n=0;
                     
            }
                     
            else
            {
                             
            JOptionPane.showMessageDialog(null,"Fail");
             
                t1.setText(" ");
                t2.setText(" ");
                n++;
                             
                if(n==3)
                System.exit(0);
                     
            }
             
}
             
        else if(e.getSource()==b2)
        {
                     
        System.exit(0);
             
        }
     
}

public static void main(String arg[])
    {
 
    ass7a2 s=new ass7a2("Login Please");
        s.setSize(400,150);
        s.setVisible(true);
        s.setLocation(200,200);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}

No comments:

Post a Comment