X.
Practical Code:
1. Write a program using JPasswordField to set the password character as ‘#’ instead of ‘*’
import java.awt.*;
import javax.swing.*;
public class PasswordFieldDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Password Field Demo");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("Enter Password:");
frame.add(label);
JPasswordField passwordField = new JPasswordField(20);
passwordField.setEchoChar('#');
frame.add(passwordField);
frame.setVisible(true);
}
}
XIII. Exercise:
1. Write a program using JPasswordField and JTextField to demonstrate the use of authentication.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleAuthDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Authentication");
frame.setSize(250, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new java.awt.FlowLayout());
JLabel userLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField(10);
JButton loginButton = new JButton("Login");
JLabel resultLabel = new JLabel("");
frame.add(userLabel);
frame.add(passwordField);
frame.add(loginButton);
frame.add(resultLabel);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String password = new String(passwordField.getPassword());
if ("password123".equals(password)) {
resultLabel.setText("Access Granted");
} else {
resultLabel.setText("Access Denied");
}
}
});
frame.setVisible(true);
}
}
2. Write a program using JTextField to perform the addition of two numbers.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleAddition {
public static void main(String[] args) {
JFrame frame = new JFrame("Addition");
frame.setSize(300, 150);
frame.setLayout(new java.awt.FlowLayout());
JLabel num1Label = new JLabel("Number 1:");
JTextField num1Field = new JTextField(8);
JLabel num2Label = new JLabel("Number 2:");
JTextField num2Field = new JTextField(8);
JButton addButton = new JButton("Add");
JLabel resultLabel = new JLabel("Result: ");
frame.add(num1Label);
frame.add(num1Field);
frame.add(num2Label);
frame.add(num2Field);
frame.add(addButton);
frame.add(resultLabel);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
resultLabel.setText("Result: " + (num1 + num2));
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid input");
}
}
});
frame.setVisible(true);
}
}
3. Write a program to verify that password length is greater than 6 or not.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PasswordLengthCheck {
public static void main(String[] args) {
JFrame frame = new JFrame("Password Length Check");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new java.awt.FlowLayout());
JLabel passwordLabel = new JLabel("Enter Password:");
JPasswordField passwordField = new JPasswordField(15);
JButton checkButton = new JButton("Check");
frame.add(passwordLabel);
frame.add(passwordField);
frame.add(checkButton);
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String password = new String(passwordField.getPassword());
if (password.length() < 6) {
JOptionPane.showMessageDialog(frame, "Password length must be greater than 6 characters");
} else {
JOptionPane.showMessageDialog(frame, "Password length is sufficient");
}
}
});
frame.setVisible(true);
}
}