Untitled
Untitled
Write a program using JPasswordField and JTextField to demonstrate the use of user
authentication
import java.awt.event.*;
import javax.swing.*;
public class JPasswordFieldDemo extends JFrame implements ActionListener {
JButton jb;
JTextField jtf;
JPasswordField jpf;
JLabel l1;
JLabel l2;
public JPasswordFieldDemo() {
setLayout(null);
setSize(350,350);
setVisible(true);
setTitle("JPasswordField and JTextField Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("User Name");
l1.setBounds(20,30,80,30);
l2=new JLabel("Password");
l2.setBounds(20,80,160,60);
jtf=new JTextField(15);
jtf.setBounds(90,30,145,30);
jpf= new JPasswordField(15);
jpf.setBounds(90,100,145,30);
jb=new JButton("Submit");
jb.setBounds(60,170,100,30);
jpf.setEchoChar('*');
add(l1);
add(jtf);
add(l2);
add(jpf);
add(jb);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String s1,s2;
s1=jtf.getText();
s2=jpf.getText();
if((s1.equals("Purvesh"))&&(s2.equals("SIOT"))) {
JOptionPane.showMessageDialog(this,"Authentication User");
}
else {
JOptionPane.showMessageDialog(this,"UnAuthorized User");
}
}
public static void main(String [] args) {
new JPasswordFieldDemo();
}
}
Output :-
2. Write a program using JTextField to perform the addition of two numbers.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JPasswordFieldDemo extends JFrame implements ActionListener
{
JButton jb;
JTextField jtf;
JPasswordField jpf;
JLabel jl1,jl2;
public JPasswordFieldDemo()
{
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
setTitle("JPasswordField using check password length Program");
jl1= new JLabel("Enter Username");
jl1.setBounds(20,20,120,20);
jl2= new JLabel("Enter Password");
jl2.setBounds(20,50,120,20);
jtf=new JTextField();
jtf.setBounds(120,20,120,20);
jpf = new JPasswordField();
jpf.setBounds(120,50,120,20);
jb=new JButton("Submit");
jb.setBounds(80,80,80,20);
add(jl1);
add(jtf);
add(jl2);
add(jpf);
add(jb);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s1;
s1=jpf.getText();
if((s1.length())<=6)
{
JOptionPane.showMessageDialog(this, "Password Length Must Be >6 Characters");
}
else if((s1.length())>=6 && (s1.length())<=12)
{
JOptionPane.showMessageDialog(this, "Password Length Accepted");
}
else
{
JOptionPane.showMessageDialog(this, "Password Length Not Accepted");
}
}
public static void main(String[]args)
{
new JPasswordFieldDemo();
}
}
Output :-