0% found this document useful (0 votes)
51 views7 pages

Ajp N12

The document contains code for a Java program that demonstrates using a password field. It creates a JFrame containing a JPasswordField and adds a key listener to change any asterisk characters typed into the field to hash symbols. The code is for a practical assignment on password fields with the student's name and practical number listed at the top.

Uploaded by

gmpawar003
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)
51 views7 pages

Ajp N12

The document contains code for a Java program that demonstrates using a password field. It creates a JFrame containing a JPasswordField and adds a key listener to change any asterisk characters typed into the field to hash symbols. The code is for a practical assignment on password fields with the student's name and practical number listed at the top.

Uploaded by

gmpawar003
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/ 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