0% found this document useful (0 votes)
15 views10 pages

AJP Experiment 12

Uploaded by

Piush Gogi
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)
15 views10 pages

AJP Experiment 12

Uploaded by

Piush Gogi
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/ 10

EXPERIMENT 12

Title: Write a program to demonstrate the use of JTextField and


JPasswordField using Listener Interface.

import javax.swing.*;
import java.awt.event.*;

public class Exp12 implements ActionListener


{
JTextField textField;
JPasswordField passwordField;
JLabel label;
Exp12()
{
JFrame f = new JFrame("JTextField and JPasswordField");

JLabel user = new JLabel("Username:");


user.setBounds(30, 30, 80, 30);
JLabel pass = new JLabel("Password:");
pass.setBounds(30, 70, 80, 30);

textField = new JTextField();


passwordField = new JPasswordField();

textField.setBounds(120, 30, 200, 30);


passwordField.setBounds(120, 70, 200, 30);

label = new JLabel();


label.setBounds(100, 150, 300, 30);

JButton submitButton = new JButton("Submit");


submitButton.setBounds(150, 110, 100, 30);

submitButton.addActionListener(this);

f.add(user);
f.add(pass);
f.add(textField);
f.add(passwordField);

Dasari N.S.
f.add(submitButton);
f.add(label);

f.setSize(400, 300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String username = textField.getText();
String password = new String(passwordField.getPassword());

label.setText("Username: " + username + ", Password: " + password);


}
public static void main(String[] args)
{
new Exp12();
}
}
Output:

Dasari N.S.
Program 12.1: Write a program to change the background color of
Applet when user performs events using Mouse.

import javax.swing.*;
import java.awt.event.*;

public class Exp12_1 implements ActionListener


{
JTextField textField;
JPasswordField passwordField;
JLabel l;
Exp12_1()
{
JFrame frame = new JFrame("JPasswordField");

JLabel userLabel = new JLabel("Username:");


userLabel.setBounds(30, 30, 80, 30);

textField = new JTextField();


textField.setBounds(120, 30, 200, 30);

JLabel passLabel = new JLabel("Password:");


passLabel.setBounds(30, 70, 80, 30);

passwordField = new JPasswordField();


passwordField.setBounds(120, 70, 200, 30);

passwordField.setEchoChar('#');

l = new JLabel();
l.setBounds(30, 150, 300, 30);

JButton submitButton = new JButton("Submit");


submitButton.setBounds(150, 110, 100, 30);
submitButton.addActionListener(this);

frame.add(userLabel);
frame.add(textField);
frame.add(passLabel);
frame.add(passwordField);

Dasari N.S.
frame.add(submitButton);
frame.add(l);

frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String username = textField.getText();
String password = new String(passwordField.getPassword());

l.setText("Username: " + username + ", Password: " + password);


}
public static void main(String[] args)
{
new Exp12_1();
}
}
Output:

Dasari N.S.
Program 12.2: Write a program using JPasswordField and JTextField to
demonstrate the use of user authentication.

import javax.swing.*;
import java.awt.event.*;

public class Exp12_2 implements ActionListener


{
JTextField textField;
JPasswordField passwordField;
JLabel l;
private final String validUsername = "admin";
private final String validPassword = "123";
Exp12_2()
{
JFrame f = new JFrame("User Authentication");

JLabel user = new JLabel("Username:");


user.setBounds(30, 30, 80, 30);
JLabel pass = new JLabel("Password:");
pass.setBounds(30, 70, 80, 30);

textField = new JTextField();


passwordField = new JPasswordField();

textField.setBounds(120, 30, 200, 30);


passwordField.setBounds(120, 70, 200, 30);

l = new JLabel();
l.setBounds(100, 150, 300, 30);

JButton submitButton = new JButton("Submit");


submitButton.setBounds(150, 110, 100, 30);
submitButton.addActionListener(this);

f.add(user);
f.add(pass);
f.add(textField);
f.add(passwordField);
f.add(submitButton);
f.add(l);

Dasari N.S.
f.setSize(400, 300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String username = textField.getText();
String password = new String(passwordField.getPassword());

if (username.equals(validUsername) &&
password.equals(validPassword))
{
l.setText("Login Successful!");
}
else
{
l.setText("Invalid Username or Password.");
}
}
public static void main(String[] args)
{
new Exp12_2();
}
}
Output:

Dasari N.S.
Program 12.3: Write a program using JTextField to perform the
addition of two numbers.

import javax.swing.*;
import java.awt.event.*;

public class Exp12_3 implements ActionListener


{
JTextField num1F;
JTextField num2F;
JLabel result;
Exp12_3()
{
JFrame f = new JFrame("Addition Calculator");

JLabel num1L = new JLabel("Number 1:");


num1L.setBounds(30, 30, 80, 30);
JLabel num2L = new JLabel("Number 2:");
num2L.setBounds(30, 70, 80, 30);

num1F = new JTextField();


num1F.setBounds(120, 30, 200, 30);
num2F = new JTextField();
num2F.setBounds(120, 70, 200, 30);

JButton add = new JButton("Add");


add.setBounds(150, 110, 100, 30);
add.addActionListener(this);

result = new JLabel();


result.setBounds(30, 150, 300, 30);

f.add(num1L);
f.add(num1F);
f.add(num2L);
f.add(num2F);
f.add(add);
f.add(result);

f.setSize(400, 250);
f.setLayout(null);

Dasari N.S.
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
try
{
int num1 = Integer.parseInt(num1F.getText());
int num2 = Integer.parseInt(num2F.getText());

int sum = num1 + num2;

result.setText("Result: " + sum);


}
catch (NumberFormatException ex)
{
result.setText("Please enter valid integers.");
}
}
public static void main(String[] args)
{
new Exp12_3();
}
}
Output:

Dasari N.S.
Program 12.4: 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 javax.swing.*;
import java.awt.event.*;

public class Exp12_4 implements ActionListener


{
JPasswordField passwordField;
JLabel resultLabel;

Exp12_4()
{
JFrame frame = new JFrame("Password Validation");

JLabel passwordLabel = new JLabel("Enter Password:");


passwordLabel.setBounds(30, 30, 120, 30);

passwordField = new JPasswordField();


passwordField.setBounds(150, 30, 150, 30);

JButton submitButton = new JButton("Submit");


submitButton.setBounds(150, 70, 100, 30);
submitButton.addActionListener(this);

resultLabel = new JLabel();


resultLabel.setBounds(30, 110, 300, 30);

frame.add(passwordLabel);
frame.add(passwordField);
frame.add(submitButton);
frame.add(resultLabel);

frame.setSize(350, 200);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

Dasari N.S.
public void actionPerformed(ActionEvent e)
{
String password = new String(passwordField.getPassword());

if (password.length() < 6)
{
resultLabel.setText("Password length must be > 6 characters");
}
else
{
resultLabel.setText("Password accepted");
}
}

public static void main(String[] args)


{
new Exp12_4();
}
}
Output:

Dasari N.S.

You might also like