import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import java.awt.event.*;
public class MyPassword implements ActionListener
{
JFrame frame;
JPanel pan,pann;
String nameoutput=" ";
String passoutput=" ";
JTextField loginField ;
JPasswordField passwordField;
JButton ok;
JButton cancel;
JLabel pict,pictlabel,loglabel,passlabel;
JScrollPane jsp;
JTextArea area;
public MyPassword()
{
frame= new JFrame();
int width=500,height=350;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x= (screen.width-width)/2;
int y= (screen.height-height)/2;
frame.setBounds(x,y,width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Testing Application");
Container contentPane = frame.getContentPane();
pann = new JPanel();
//pann.setLayout(new FlowLayout(FlowLayout.CENTER));
//pann.setLayout(new GridLayout(2,1));
pann.setLayout(new BorderLayout());
pann.setOpaque(true);
pann.setBorder(BorderFactory.createLineBorder(Color.RED));
pan =new JPanel();
pan.setLayout(new GridLayout(4,2,20,10));
pan.setBackground(new Color(202,215,237));
pan.setOpaque(true);
pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pict=new JLabel("Login",new ImageIcon("icons/login.gif"),JLabel.CENTER);
pan.add(pict);
pictlabel= new JLabel("Reservation System");
pictlabel.setFont( new Font( "Times New Roman", Font.BOLD,20 ));
pictlabel.setForeground(Color.red);
pan.add(pictlabel);
loglabel=new JLabel("Enter Login Name");
loglabel.setFont( new Font( "Times New Roman", Font.BOLD,14 ) );
loglabel.setForeground(Color.blue);
pan.add(loglabel);
loginField = new JTextField(10);
loginField.setFont( new Font( "Times New Roman", Font.BOLD,14 ) );
loginField.setToolTipText( " Enter ur Login Name ");
loginField.addActionListener(this);
pan.add(loginField);
passlabel= new JLabel("Enter Your Password");
passlabel.setFont( new Font( "Times New Roman", Font.BOLD,16 ) );
passlabel.setForeground(Color.blue);
pan.add(passlabel);
passwordField = new JPasswordField(10);
passwordField.setToolTipText( " Enter ur Password ");
passwordField.setFont( new Font( "Impact", Font.BOLD, 16) );
passwordField.setEchoChar('o');
passwordField.addActionListener(this);
pan.add(passwordField);
ok= new JButton("OK");
ok.setBorder(BorderFactory.createRaisedBevelBorder());
ok.addActionListener(this);
pan.add(ok);
cancel= new JButton("Cancel");
cancel.setBorder(BorderFactory.createRaisedBevelBorder());
cancel.addActionListener(this);
pan.add(cancel);
pann.add(pan, BorderLayout.NORTH);
//pann.setBackground(new Color(202,215,237));
area = new JTextArea(100,50);
jsp= new JScrollPane(area);
pann.add(jsp,BorderLayout.CENTER);
contentPane.add(pann);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String Oldpassword="rana";
String Currentpassword=Oldpassword;
String Newpassword=" ";
String Oldname="imrankhan";
String Currentname=Oldname;
String Newname=" ";
if(e.getSource() == ok)
{
Currentname = loginField.getText();
Currentpassword = new String(passwordField.getPassword()); //passwordField.getText();
if(Currentpassword.equals(Oldpassword) && Currentname.equals(Oldname))
{
area.append(Currentpassword +'\n');
area.append(Currentname +'\n');
JOptionPane.showMessageDialog(null,"Your Passward is Ok");
}
else {
area.append(Currentname +'\n');
area.append(Currentpassword +'\n');
JOptionPane.showMessageDialog(null, "Invalid password.or User name! Try again.",
"Error Message", JOptionPane.ERROR_MESSAGE);
}
}
if(e.getSource() == cancel)
{
System.exit(0);
}
}
public static void main (String []arg)
{
MyPassword f = new MyPassword();
}
}
/*
private boolean isPasswordCorrect(char[] input) {
char[] correctPassword = { 'i', 'm', 'r', 'a', 'n', 'k', 'h', 'a','n' };
if (input.length != correctPassword.length)
return false;
for (int i = 0; i < input.length; i ++)
if (input[i] != correctPassword[i])
return false;
return true;
}
void correctPassword(String pas);
{
String correctPassword = new String("Have a nice Day!");
String incorrectPassword = new String("Opps! Try again!");
JPasswordField input = (JPasswordField)pas.getSource();
char[] password = input.getPassword();
if (isPasswordCorrect(password))
{
//textfield.setText(correctPassword);
JOptionPane.showMessageDialog(option, "Success! You typed the right password.");
MyMainFrame1 fr = new MyMainFrame1();
fr.show();
}
else {
//textfield.setText(incorrectPassword);
JOptionPane.showMessageDialog(option, "Invalid password. Try again.",
"Error Message", JOptionPane.ERROR_MESSAGE);
}
}
*/
0 comments: