PRACTICAL 12
import javax.swing.*;
import java.awt.*;
public class P101 {
private JFrame frame;
private JPasswordField passwordField;
public P101() {
frame = new JFrame("JPasswordField Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
passwordField = new JPasswordField(20);
passwordField.setEchoChar('#');
JPanel panel = new JPanel();
panel.add(new JLabel("Password:"));
panel.add(passwordField);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
public static void main(String[] args) {
new P101();
OUTPUT:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class p122 {
private JFrame frame;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private JLabel messageLabel;
public p122() {
frame = new JFrame("User Authentication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
usernameField = new JTextField(15);
passwordField = new JPasswordField(15);
passwordField.setEchoChar('#');
loginButton = new JButton("Login");
messageLabel = new JLabel();
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("user") && password.equals("password")) {
messageLabel.setText("Login successful!");
} else {
messageLabel.setText("Invalid username or password.");
}
}
});
JPanel panel = new JPanel();
panel.add(new JLabel("Username:"));
panel.add(usernameField);
panel.add(new JLabel("Password:"));
panel.add(passwordField);
panel.add(loginButton);
panel.add(messageLabel);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
public static void main(String[] args) {
new p122();
OUTPUT:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class p123 {
private JFrame frame;
private JTextField num1Field;
private JTextField num2Field;
private JLabel resultLabel;
private JButton addButton;
public p123() {
frame = new JFrame("Addition of Two Numbers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
num1Field = new JTextField(10);
num2Field = new JTextField(10);
resultLabel = new JLabel("Result: ");
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double sum = num1 + num2;
resultLabel.setText("Result: " + sum);
} catch (NumberFormatException ex) {
resultLabel.setText("Please enter valid numbers.");
});
JPanel panel = new JPanel();
panel.add(new JLabel("Number 1:"));
panel.add(num1Field);
panel.add(new JLabel("Number 2:"));
panel.add(num2Field);
panel.add(addButton);
panel.add(resultLabel);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
public static void main(String[] args) {
new p123();
OUTPUT:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class p124 {
private JFrame frame;
private JPasswordField passwordField;
private JButton submitButton;
private JLabel messageLabel;
public p124() {
frame = new JFrame("Password Validation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
passwordField = new JPasswordField(15);
passwordField.setEchoChar('#');
submitButton = new JButton("Submit");
messageLabel = new JLabel();
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String password = new String(passwordField.getPassword());
if (password.length() < 6) {
messageLabel.setText("Error: Password must be at least 6
characters long.");
} else {
messageLabel.setText("Password accepted.");
});
JPanel panel = new JPanel();
panel.add(new JLabel("Password:"));
panel.add(passwordField);
panel.add(submitButton);
panel.add(messageLabel);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
public static void main(String[] args) {
new p124();
OUTPUT: