/**
* @(#)User_password.java
*
*
* @author -> anant mahale
* @version 1.00 2013/8/29
*
*write a program that displays a dialog box and accept username and password from the user. the
*information entered by the user should be displayed on the screen on the client area.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class User_password
{
public static void main(String args[])
{
myframe m=new myframe();
m.setTitle("User Password Program");
m.setSize(400,400);
m.setLocation(150,150);
m.setVisible(true);
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class myframe extends JFrame
{
mypanel p;
myframe()
{
p=new mypanel();
Container c=getContentPane();
c.add(p);
}
}
class mypanel extends JPanel implements ActionListener
{
mydialog d;
JButton btn_clik;
JLabel lbl_usr,lbl_pass,lbl_msg1,lbl_msg2;
mypanel()
{
setLayout(null);
btn_clik=new JButton("Click Here for LogIn");
btn_clik.setBounds(100,50,200,30);
btn_clik.addActionListener(this);
add(btn_clik);
lbl_msg1=new JLabel();
lbl_msg1.setForeground(Color.green);
lbl_msg1.setFont(new Font("Arial",Font.BOLD,20));
lbl_msg1.setBounds(100,100,325,30);
add(lbl_msg1);
lbl_msg2=new JLabel();
lbl_msg2.setForeground(Color.red);
lbl_msg2.setFont(new Font("Arial",Font.BOLD,20));
lbl_msg2.setBounds(100,100,325,30);
add(lbl_msg2);
lbl_usr=new JLabel();
lbl_usr.setForeground(Color.green);
lbl_usr.setFont(new Font("Arial",Font.BOLD,20));
lbl_usr.setBounds(100,150,325,30);
add(lbl_usr);
lbl_pass=new JLabel();
lbl_pass.setForeground(Color.green);
lbl_pass.setFont(new Font("Arial",Font.BOLD,20));
lbl_pass.setBounds(100,200,325,30);
add(lbl_pass);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_clik)
{
d=new mydialog();
d.setVisible(true);
String str_usr=d.usrval();
String str_pas=d.passval();
if(str_usr.equals("Anant")&&str_pas.equals("mahale"))
{
lbl_msg2.setText("");
lbl_msg1.setText("LogIn Successful");
lbl_usr.setText("User Name - "+str_usr);
lbl_pass.setText("Password - "+str_pas);
}
else
{
lbl_msg1.setText("");
lbl_usr.setText("");
lbl_pass.setText("");
lbl_msg2.setText("Wrong User...");
}
}
}
}
class mydialog extends JDialog implements ActionListener
{
JButton btn_login,btn_exit;
JLabel lbl_usr,lbl_pass,lbl_pass_agn,lbl_heads,lbl_err;
JTextField txt_usr;
JPasswordField txt_pass,txt_pass_agn;
String str_usr="",str_pass="";
mydialog()
{
super(new myframe(),"Enter User Name & Password",true);
setLayout(null);
setSize(400,350);
setLocation(450,200);
lbl_heads=new JLabel("Enter User Name & Password");
lbl_heads.setBounds(100,10,200,30);
add(lbl_heads);
lbl_usr=new JLabel("User Name");
lbl_usr.setBounds(50,50,100,30);
add(lbl_usr);
txt_usr=new JTextField();
txt_usr.setBounds(175,50,150,30);
add(txt_usr);
lbl_pass=new JLabel("Password");
lbl_pass.setBounds(50,100,100,30);
add(lbl_pass);
txt_pass=new JPasswordField();
txt_pass.setBounds(175,100,150,30);
add(txt_pass);
lbl_pass_agn=new JLabel("Password Again");
lbl_pass_agn.setBounds(50,150,100,30);
add(lbl_pass_agn);
txt_pass_agn=new JPasswordField();
txt_pass_agn.setBounds(175,150,150,30);
add(txt_pass_agn);
btn_login=new JButton("Login");
btn_login.setBounds(100,200,75,30);
btn_login.addActionListener(this);
add(btn_login);
btn_exit=new JButton("Exit");
btn_exit.setBounds(200,200,75,30);
btn_exit.addActionListener(this);
add(btn_exit);
lbl_err=new JLabel();
lbl_err.setForeground(Color.red);
lbl_err.setFont(new Font("Arial",Font.BOLD,20));
lbl_err.setBounds(25,250,325,30);
add(lbl_err);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_login)
{
String str_pas1=txt_pass.getText();
String str_pas2=txt_pass_agn.getText();
if(str_pas1.equals(str_pas2))
{
str_usr=txt_usr.getText();
str_pass=txt_pass.getText();
dispose();
}
else
{
lbl_err.setText("Both Passwords are not match...");
txt_pass.setText("");
txt_pass_agn.setText("");
txt_usr.setText("");
}
}
if(ae.getSource()==btn_exit)
{
System.exit(0);
}
}
public String usrval()
{
return str_usr;
}
public String passval()
{
return str_pass;
}
}
No comments:
Post a Comment