12
12
1.WAP using JPasswordField to set the password character as ‘#’ instead of ‘*’.
import java.awt.*;
import javax.swing.*;
public class Expt_12_1 {
public static void main(String args[])
{
JFrame f=new JFrame();
f.setSize(400,400);
f.setVisible(true);
f.setLayout(new FlowLayout());
JPasswordField pf=new JPasswordField(20);
pf.setEchoChar('#');
f.add(pf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT:
2.WAP using JPasswordField and JTextField to demonstrate the use of user authentication.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Expt_12_2 extends JFrame implements ActionListener
{
JTextField tf;
JPasswordField tff;
JButton b1;
public Expt_12_2()
{
JLabel j1 = new JLabel("Enter Username");
tf = new JTextField(15);
JLabel j11 = new JLabel("Enter Password");
tff = new JPasswordField(15);
b1 = new JButton("Submit");
setLayout(new FlowLayout());
add(j1);
add(tf);
add(j11);
add(tff);
add(b1);
b1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
public static void main(String args[])
{
new Expt_12_2();
}
public void actionPerformed(ActionEvent e)
{
String s1 = tf.getText();
String s2 = new String(tff.getPassword());
if (s1.equals("SHASHWAT") && s2.equals("GANDHI"))
{
JOptionPane.showMessageDialog(this, "Authenticate user");
}
else
{
JOptionPane.showMessageDialog(this, "Unauthenticate user");
}
}
}
OUTPUT:
3. WAP using JTextField to perform the addition of two numbers.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Expt_12_3 extends JFrame implements ActionListener
{
JTextField tf,tff,tf3;
JButton b1;
public Expt_12_3()
{
JLabel j1 = new JLabel("Enter Number 1: ");
tf = new JTextField(3);
JLabel j11 = new JLabel("Enter Number 2: ");
tff = new JTextField(3);
b1 = new JButton("Add");
tf3 = new JTextField(3);
setLayout(new FlowLayout());
add(j1);
add(tf);
add(j11);
add(tff);
add(b1);
add(tf3);
b1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
public static void main(String args[]) {
new Expt_12_3();
}
public void actionPerformed(ActionEvent e) {
int i=Integer.parseInt(tf.getText());
int j=Integer.parseInt(tff.getText());
int z=i+j;
tf3.setText(""+z);}}
OUTPUT:
4.WAP 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
characters”.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Expt_12_4 extends JFrame implements ActionListener
{
JTextField tf;
JPasswordField tff;
JButton b1;
public Expt_12_4()
{
JLabel j1 = new JLabel("Enter Username");
tf = new JTextField(15);
JLabel j11 = new JLabel("Enter Password");
tff = new JPasswordField(15);
b1 = new JButton("Submit");
setLayout(new FlowLayout());
add(j1);
add(tf);
add(j11);
add(tff);
add(b1);
b1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
public static void main(String args[])
{
new Expt_12_4();
}
public void actionPerformed(ActionEvent e)
{
String s1 = tf.getText();
String s2 = new String(tff.getPassword());
int l=s2.length();
if (l>6)
{
JOptionPane.showMessageDialog(this, "Authenticate user");
}
else
{
JOptionPane.showMessageDialog(this, "Password length must be >6 characters");
}
}
}
OUTPUT: