0% found this document useful (0 votes)
9 views

Ajp Exp 12

AJP practical output prints

Uploaded by

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

Ajp Exp 12

AJP practical output prints

Uploaded by

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

EXPERIMENT NO: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:

2. Write a program using JTextField to perform the addition of two numbers.


Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class program extends JFrame implements ActionListener {
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1,b2;
program() {
setLayout(new GridLayout(4,2));
l1=new JLabel("Enter the first number:");
t1=new JTextField(10);
l2=new JLabel("Enter the second number:");
t2=new JTextField(10);
l3=new JLabel("Addition of number's is:");
t3=new JTextField(10);
b1=new JButton("Add");
b1.addActionListener(this);
b2=new JButton("Clear");
b2.addActionListener(this);
add(l1); add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
setVisible(true);
setSize(100,100);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1) {
int c=Integer.parseInt(t1.getText());
int d=Integer.parseInt(t2.getText());
int a=c+d;
t3.setText(Integer.toString(a));
}
if (e.getSource()==b2) {
EXPERIMENT NO:12
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
}
}
public static void main(String []args) {
new program();
}
}

OUTPUT:

You might also like