0% found this document useful (0 votes)
38 views4 pages

Ajp 10

The document describes a Java program that implements a login authentication system using a username, password, and login button. When the login button is clicked, it checks if the entered username and password match the hardcoded credentials, and displays an appropriate message. It uses Swing components like JFrame, JPanel, JLabel, JTextField, JPasswordField and JButton to build the GUI.

Uploaded by

Ganesh Ekambe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Ajp 10

The document describes a Java program that implements a login authentication system using a username, password, and login button. When the login button is clicked, it checks if the entered username and password match the hardcoded credentials, and displays an appropriate message. It uses Swing components like JFrame, JPanel, JLabel, JTextField, JPasswordField and JButton to build the GUI.

Uploaded by

Ganesh Ekambe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name of Student: Ekambe Ganesh Roll No.

: 53

Experiment No.: 11 DOS:

Program code:
1. Write a program using JPasswordField to set the password
character as ‘#’ instead of ‘*’

package advancejava;

import javax.swing.*;
import java.awt.*;

public class pr12


{
public static void main(String[] args) {
JFrame f = new JFrame();

f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());

JPasswordField pf = new JPasswordField(20);

pf.setEchoChar('#');

f.add(pf);
}
}

Output
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 11 DOS:

Exercise:

1.

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

public class pr12ex2 {


private static final String USERNAME = "Ganesh";
private static final String PASSWORD = "pass123";

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("User Authentication Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);

JPanel panel = new JPanel();


panel.setLayout(new GridLayout(3, 2));

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


JTextField usernameField = new JTextField(15);

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


JPasswordField passwordField = new JPasswordField(15);

JButton loginButton = new JButton("Login");

loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String enteredUsername = usernameField.getText();
char[] enteredPassword = passwordField.getPassword();

if (USERNAME.equals(enteredUsername) &&
PASSWORD.equals(new String(enteredPassword))) {
JOptionPane.showMessageDialog(frame, "Login
Successful!");
} else {
JOptionPane.showMessageDialog(frame, "Login
Failed. Invalid credentials.", "Error", JOptionPane.ERROR_MESSAGE);
}

passwordField.setText("");
}
});

panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel()); //
panel.add(loginButton);
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 11 DOS:

frame.add(panel);
frame.setVisible(true);
});
}
}

Output

1.

package advancejava;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class pr12ex1 implements ActionListener
{
JTextField tf , tf1 ;
JLabel res;

pr12ex1()
{
JFrame f = new JFrame();

f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());

JLabel jl = new JLabel("Enter 1st Number:");


tf = new JTextField(5);
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 11 DOS:


JLabel jl1 = new JLabel("Enter 2nd Number:");
tf1 = new JTextField(5);
res = new JLabel("Addition");

tf1.addActionListener(this);

f.add(jl);
f.add(tf);
f.add(jl1);
f.add(tf1);
f.add(res);

}
public static void main(String[] args) {
pr12ex1 jt = new pr12ex1();
}

public void actionPerformed(ActionEvent ae)


{
String str1 = tf.getText();
double fn = Double.parseDouble(str1);

double sn = Double.parseDouble(tf1.getText());

res.setText("Sum is " + (fn+sn));


}
}

You might also like