CODE:
package swing_practicals;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginForm extends JFrame implements ActionListener{
JLabel lblname,lblpwd;
JTextField txtname;
JPasswordField txtpwd;
JButton btnok,btncancel;
JPanel p1;
public LoginForm(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(300,200);
setVisible(true);
lblname = new JLabel("Enter Your Name:");
txtname = new JTextField(15);
lblpwd = new JLabel("Enter Password:");
txtpwd = new JPasswordField("",15);
p1 = new JPanel();
p1.setLayout(new GridLayout(3,2));
btnok = new JButton("Ok");
btncancel = new JButton("Cancel");
btnok.addActionListener(this);
btncancel.addActionListener(this);
p1.add(lblname);
p1.add(txtname);
p1.add(lblpwd);
p1.add(txtpwd);
p1.add(btnok);
p1.add(btncancel);
getContentPane().add(p1,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
char[] txt = txtpwd.getPassword();
String strin0 = txtname.getText();
String string1 = new String(txt);
if(e.getSource() == btnok){
if(strin0.equals("Kiran")&&string1.equals("abc")){
JOptionPane.showMessageDialog(null,"Login Successful.","login",1);
}
else{
JOptionPane.showMessageDialog(null,"Login Unsuccessful.","login",2);
txtname.setText(" ");
txtpwd.setText("");
}
}
if(e.getSource() == btncancel){
System.exit(0);
}
}
public static void main(String[] args) {
new LoginForm();
}
}
OUTPUT:
0 Comments