0% found this document useful (0 votes)
3 views

practical programs

Uploaded by

ayushdesai151
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)
3 views

practical programs

Uploaded by

ayushdesai151
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

1.

Write a program using JPasswordField to accept password from user


and if the

length is less than 6 characters then error message should be displayed


“Password length must be >6 characters”

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Exp12_Ex3 implements ActionListener {

JFrame f;

JLabel res, u_nm, pass_word;

JPasswordField tp;

JTextField tt;

public Exp12_Ex3() {

f = new JFrame();

f.setLayout(new FlowLayout());

u_nm = new JLabel("User Name");

tt = new JTextField(10);

pass_word = new JLabel("Password");

tp = new JPasswordField(10);

JButton sub = new JButton("Submit");

sub.addActionListener(this);

res = new JLabel();

f.add(u_nm);

f.add(tt);

f.add(pass_word);

f.add(tp);

f.add(sub);

f.add(res);

f.setSize(250, 200);
f.setTitle("243226");

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public static void main(String[] args) {

new Exp12_Ex3();

@Override

public void actionPerformed(ActionEvent e) {

// Retrieve the password as a character array and convert it to a


String

String password = new String(tp.getPassword());

// Check if the password is longer than 6 characters

if (password.length() > 6) {

res.setText("Password is valid.");

} else {

res.setText("Password must be longer than 6 characters.");

2. Write a program to change the background color of Applet when user

performs events using Mouse


import java.awt .*;

import java.awt.event .*;

import java.util.Random;

import java.applet .*;

public class Exp11_Ex1 extends Applet implements MouseListener

Color clr;

public void init()

addMouseListener(this);

public void paint(Graphics g)

setBackground(clr);

@Override

public void mouseClicked(MouseEvent argo) {

Random rand = new Random();

int redValue = rand.nextInt(255); int greenValue = rand.nextInt(255); int


blueValue = rand.nextInt(255);

clr = new Color(redValue, greenValue, blueValue);

repaint();

@Override

public void mouseEntered(MouseEvent argo) (}

@Override

public void mouseExited(MouseEvent e) (}

@Override

public void mousePressed(MouseEvent e) (}


@Override

public void mouseReleased(MouseEvent e) (}

//<applet code="Exp11_Ex1" width=200 height=300 > </applet>

You might also like