[go: up one dir, main page]

0% found this document useful (0 votes)
22 views14 pages

R.Practical 12th

Ajp practical no. 12
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)
22 views14 pages

R.Practical 12th

Ajp practical no. 12
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/ 14

Subject - AJP [22517]

Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

X - PROGRAM CODE

Q ------------------------

------------------------------------------- PROGRAM CODE -----------------------------------------------------------

import javax.swing.*;

import java.awt.*;

public class P12XCode extends JPasswordField

public P12XCode(int columns)

super(columns);

@Override

public char getEchoChar()

return '#';

public static void main(String[] args)

JFrame f = new JFrame("To Set The Password Character As '#' instead of '*' ");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLayout(new FlowLayout());

P12XCode pf = new P12XCode(20);

pf.setEchoChar('#');

JButton PB = new JButton("Show Password");

PB.addActionListener(e ->

{
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

char echoChar = pf.getEchoChar();

if (echoChar == 0)

pf.setEchoChar('#');

else

pf.setEchoChar((char) 0);

});

JButton B1 = new JButton("Hide Password");

B1.addActionListener(e ->

char echoChar = pf.getEchoChar();

if (echoChar == -1)

pf.setEchoChar((char) 0);

else

pf.setEchoChar('#');

});

f.add(pf);

f.add(PB);

f.add(B1);
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

f.pack();

f.setVisible(true);

f.setSize(500, 200);

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------


Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

XIII. Exercise

Q1 ------------------------

------------------------------------------- PROGRAM CODE -----------------------------------------------------------

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class P12ExQ1

private static final String VALID_USERNAME = "Atharva Bodhankar";

private static final String VALID_PASSWORD = "mustang1969";

public static void main(String[] args)

JFrame f = new JFrame("User Authentication Demo");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLayout(new BorderLayout());

JPanel logP = new JPanel(new GridLayout(3, 2, 20, 20));

JLabel UL = new JLabel("User-name:");

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

JTextField userfi = new JTextField();

JPasswordField pf = new JPasswordField();

JButton logb = new JButton("Log in");

logb.addActionListener(new ActionListener()

@Override
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

public void actionPerformed(ActionEvent e)

String username = userfi.getText();

char[] pchar = pf.getPassword();

String password = new String(pchar);

if (isValidUser(username, password))

JOptionPane.showMessageDialog(f, "Login Successful!");

else

JOptionPane.showMessageDialog(f, "Invalid Username or Password", "Error",


JOptionPane.ERROR_MESSAGE);

pf.setText("");

});

logP.add(UL);

logP.add(userfi);

logP.add(pL);

logP.add(pf);

logP.add(logb);

f.add(logP, BorderLayout.CENTER);

f.setSize(300, 200);

f.setLocationRelativeTo(null);

f.setVisible(true);
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

private static boolean isValidUser(String username, String password)

return username.equals(VALID_USERNAME) &&

password.equals(VALID_PASSWORD);

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------


Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

Q2 ------------------------

------------------------------------------- PROGRAM CODE -----------------------------------------------------------

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class P12ExQ2

public static void main(String[] args)

JFrame f = new JFrame("To Perform The Addition Of Two Numbers");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLayout(new BorderLayout());

JPanel P1 = new JPanel(new GridLayout(2, 2, 10, 10));

JTextField f1 = new JTextField();

JTextField f2 = new JTextField();

JButton b1 = new JButton("Add The Numbers ");

JTextField f3 = new JTextField();

b1.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

try

{
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

double num1 = Double.parseDouble(f1.getText());

double num2 = Double.parseDouble(f2.getText());

double num3 = num1 + num2;

f3.setText(Double.toString(num3));

catch (NumberFormatException ex)

f3.setText("Invalid input");

});

P1.add(new JLabel("1st Number : "));

P1.add(f1);

P1.add(new JLabel("2nd Number : "));

P1.add(f2);

f.add(P1, BorderLayout.NORTH);

f.add(b1, BorderLayout.EAST);

f.add(f3, BorderLayout.SOUTH);

f3.setEditable(false);

f.setSize(400, 150);

f.setLocationRelativeTo(null);

f.setVisible(true);

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------


Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

Q3 ------------------------

------------------------------------------- PROGRAM CODE -----------------------------------------------------------

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class P12ExQ3

public static void main(String[] args)

JFrame f = new JFrame("To Accept Password From User With Six Charecters");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLayout(new BorderLayout());

JPanel pan = new JPanel(new FlowLayout());

JPasswordField pf = new JPasswordField(20);

JButton sb = new JButton("Submit");

sb.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

{
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

char[] pc = pf.getPassword();

String password = new String(pc);

if (password.length() < 6)

JOptionPane.showMessageDialog(f, "Ivalid Password - Please Enter Six or More


Charecters", "Error",

JOptionPane.ERROR_MESSAGE);

else

JOptionPane.showMessageDialog(f, " Valid Password !");

pf.setText("");

});

pan.add(new JLabel("Enter Password:"));

pan.add(pf);

pan.add(sb);

f.add(pan, BorderLayout.CENTER);

f.setSize(300, 150);

f.setLocationRelativeTo(null);

f.setVisible(true);

f.setSize(300, 200);

}
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------


Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Pranav Jawadwar
CO5I – 11
_________________________________________________________________________________________________________________________________

You might also like