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

Untitled

The document contains 3 code examples demonstrating the use of JPasswordField and JTextField in Java Swing applications: 1. The first example shows user authentication using a JPasswordField and JTextField to check the entered password and username. 2. The second example performs numeric addition of two numbers entered in JTextFields and displays the result. 3. The third example uses a JPasswordField to accept a password from the user and displays an error message if the password length is less than 6 characters.

Uploaded by

Pagocar
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)
67 views6 pages

Untitled

The document contains 3 code examples demonstrating the use of JPasswordField and JTextField in Java Swing applications: 1. The first example shows user authentication using a JPasswordField and JTextField to check the entered password and username. 2. The second example performs numeric addition of two numbers entered in JTextFields and displays the result. 3. The third example uses a JPasswordField to accept a password from the user and displays an error message if the password length is less than 6 characters.

Uploaded by

Pagocar
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/ 6

1.

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.*;

public class JTextFieldDemo extends JFrame implements ActionListener {


JLabel l1;
JTextField t1;
JLabel l2;
JTextField t2;
JButton b;
JLabel l3;
JTextField t3;
public JTextFieldDemo() {
setVisible(true);
setSize(300, 300);
setTitle("JTextField Demo");
setBackground(Color.yellow);
setLayout(null);
l1 = new JLabel("1st Number");
l1.setBounds(10, 30, 80, 30);
t1 = new JTextField(5);
t1.setBounds(100, 40, 80, 20);
l2 = new JLabel("2nd Number");
l2.setBounds(10, 65, 80, 30);
t2 = new JTextField(5);
t2.setBounds(100, 70, 80, 20);
b = new JButton("Addition");
b.setBounds(70, 140, 80, 30);
l3 = new JLabel("Result");
l3.setBounds(10, 95, 80, 30);
t3 = new JTextField(5);
t3.setBounds(100, 100, 80, 20);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(t1.getText());
int num2 = Integer.parseInt(t2.getText());
int value = num1 + num2;
t3.setText("" + value);
}
public static void main(String [] args) {
new JTextFieldDemo();
}
}
Output :-
3. Write a 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 characters”

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

You might also like