0% found this document useful (0 votes)
12 views6 pages

12

The document contains four Java Swing experiments demonstrating the use of JPasswordField and JTextField for various applications, including setting custom password characters, user authentication, addition of numbers, and password length validation. Each experiment includes code snippets and outlines the functionality of the graphical user interface. The primary focus is on user input handling and validation using event listeners.

Uploaded by

dnyvlogs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

12

The document contains four Java Swing experiments demonstrating the use of JPasswordField and JTextField for various applications, including setting custom password characters, user authentication, addition of numbers, and password length validation. Each experiment includes code snippets and outlines the functionality of the graphical user interface. The primary focus is on user input handling and validation using event listeners.

Uploaded by

dnyvlogs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No-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:

You might also like