You are on page 1of 7

Name: Shaunak Kompalwar

Practical no: 12

Code:

import javax.swing.*;

import java.awt.event.KeyEvent;

importjava.awt.event.KeyListener;

public class PasswordFieldDemo {

public static void main(String[]

args) {

JFrame frame = new

JFrame("Password Field

Demo");

frame.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 100);

JPasswordField passwordField

= new JPasswordField(20); passwordField.addKeyListener(n ew

KeyListener()

public void
keyTyped(KeyEvent e) {

char c = e.getKeyChar();

if (c == '*') {

e.setKeyChar('#');

public void

keyPressed(KeyEvent e) {

public void

keyReleased(KeyEvent e)

});

frame.add(passwordField);

frame.setVisible(true);

Output:
Code:

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

public class UserAuthenticationDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("User Authentication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setLayout(new GridLayout(3, 2));

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


JTextField usernameField = new JTextField();

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


JPasswordField passwordField = new JPasswordField();

JButton loginButton = new JButton("Login");

loginButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


String username = usernameField.getText();
char[] passwordChars = passwordField.getPassword();
String password = new String(passwordChars);

if (authenticate(username, password)) {
JOptionPane.showMessageDialog(frame, "Login Successful!");
} else {
JOptionPane.showMessageDialog(frame, "Login Failed. Please try
again.");
}

passwordField.setText("");
}
});
frame.add(usernameLabel);
frame.add(usernameField);
frame.add(passwordLabel);
frame.add(passwordField); frame.add(new
JLabel());
frame.add(loginButton);

frame.setVisible(true);
}

private static boolean authenticate(String username, String password) {

String correctUsername = "user"; String


correctPassword = "password"; return
username.equals(correctUsername) &&
password.equals(correctPassword);
}
}

Output:
Code:

import javax.swing.*; import


java.awt.*; import
java.awt.event.ActionEvent; import
java.awt.event.ActionListener;

public class SimpleAdditionCalculator {


public static void main(String[] args) {
JFrame frame = new JFrame("Simple Addition Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setLayout(new GridLayout(3, 2));

JLabel num1Label = new JLabel("Enter Number 1:");


JTextField num1Field = new JTextField();

JLabel num2Label = new JLabel("Enter Number 2:");


JTextField num2Field = new JTextField();

JButton addButton = new JButton("Add");


JLabel resultLabel = new JLabel("Result:");

addButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = num1 + num2;
resultLabel.setText("Result: " + result); }
catch (NumberFormatException ex) {
resultLabel.setText("Result: Invalid input");
}
}
});
frame.add(num1Label);
frame.add(num1Field); frame.add(num2Label);
frame.add(num2Field); frame.add(addButton);
frame.add(resultLabel);

frame.setVisible(true);
}
}

Output:

You might also like