Ajp Exp 12 Op
Ajp Exp 12 Op
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);
}
}