Ajp Exp 12
Ajp Exp 12
X Program Code:
Q. Program using JPasswordField and JTextField to demonstrate the use of user
authentication.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class program extends JFrame implements ActionListener {
JPasswordField j;
JLabel l1, l2, l3;
JTextField t1;
JButton b1, b2;
program() {
setLayout(new GridLayout(4, 2));
l1 = new JLabel("Enter username: ");
l2 = new JLabel("Enter password: ");
t1 = new JTextField(10);
j = new JPasswordField(10);
l3 = new JLabel(" ");
b1 = new JButton("Login");
b2 = new JButton("Reset");
b1.addActionListener(this);
b2.addActionListener(this);
add(l1); add(t1);
add(l2); add(j);
add(b1); add(b2);
add(l3); setSize(400, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (t1.getText().equals("mhssp") && j.getText().equals("mhssp"))
l3.setText("Valid Login");
else l3.setText("Invalid Login");
}
else if (e.getSource() == b2) {
t1.setText(" ");
j.setText(" ");
l3.setText(" ");
}}
public static void main(String a[]){ new program();}
OUTPUT:
EXPERIMENT NO:12
XIII EXERCISE:
3. Program using JPasswordField to accept password from user and if the length is
less than 6 characters then error message should be displayed “Password length must
be > 6”
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class program extends JFrame implements ActionListener
{
JPasswordField j;
JLabel l1, l2, l3;
JTextField t1;
JButton b1, b2;
program() {
setLayout(new GridLayout(4, 2));
l1 = new JLabel("Enter username: ");
l2 = new JLabel("Enter password: ");
t1 = new JTextField(10);
j = new JPasswordField(10);
l3 = new JLabel(" ");
b1 = new JButton("Login");
b2 = new JButton("Reset");
b1.addActionListener(this);
b2.addActionListener(this);
add(l1);
add(t1);
add(l2);
add(j);
add(b1);
add(b2);
add(l3);
setSize(400, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (t1.getText().equals("mhssp") && j.getText().length()<6)
l3.setText("Password length must be greater than 6 characters");
EXPERIMENT NO:12
else
l3.setText("Valid Password");
}
else if (e.getSource() == b2) {
t1.setText(" ");
j.setText(" ");
l3.setText(" ");
}
}
public static void main(String a[]) { new program();}
}
OUTPUT:
1. Write a program using JPasswordField to set the password character as ‘#’ instead
of ‘*’.
Code:
import javax.swing.*;
import java.awt.*;
public class program extends JFrame
{
JLabel l1;
JPasswordField pf1;
program()
{
setLayout(new FlowLayout());
l1=new JLabel("Enter the Password");
pf1=new JPasswordField(10);
pf1.setEchoChar('#');
add(l1);
add(pf1);
setVisible(true);
setSize(100,100);
}
public static void main(String []args)
{
new program();
}
}
EXPERIMENT NO:12
Output:
OUTPUT: