dd
NAME: SUMEDH SANJAY RAUT
CLASS: CO5I(A)
ROLL NO: 532
SUBJECT: AJP(22517)
*PRATICAL NO :11
X. Program Code: Teacher must assign a separate program statement to group of 3-4
students.
1. Debug the following Program code and write the output
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/*
<applet code="PRA11c" width=300 height=300>
</applet>
*/
public class PRA11c extends Applet implements MouseListener
{
Label l;
public void init()
{
setLayout(null);
l=new Label("Hello Mouse");
l.setBounds(50,150,200,100);
add(l);
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
l.setText("Mouse Pressed No. of clicks:" +e.getClickCount() +"at position" +e.getX()+ ","
+e.getY());
}
public void mouseReleased(MouseEvent e)
{
l.setText("Mouse Released; # of of clicks:" +e.getClickCount());
}
public void mouseEntered(MouseEvent e)
{
dd
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
l.setText("Mouse Exited");
}
public void mouseClicked(MouseEvent e)
{
l.setText("Mouse Clicked; # of clicks:" +e.getClickCount());
}
}
Output:-
dd
Exercise:-
Q.1) Write a program to change the background color of Applet when user performs events
using Mouse
Code:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Authentication extends JFrame implements ActionListener {
JButton button;
JTextField jtf;
JPasswordField jpf;
JLabel jl, j2;
public Authentication() {
setVisible(true);
setTitle("Authentication Frame");
setSize(300, 300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl = new JLabel("User Name:");
j2 = new JLabel("Password:");
jtf = new JTextField(15);
jpf = new JPasswordField(15);
button = new JButton("Submit");
add(jl);
add(jtf);
add(j2);
add(jpf);
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String s1, s2;
s1 = jtf.getText();
s2 = new String(jpf.getPassword());
if (s1.equals("ABC") && s2.equals("123")) {
JOptionPane.showMessageDialog(this, "Authenticated User");
} else {
dd
JOptionPane.showMessageDialog(this, "Unauthorized User");
}
}
public static void main(String[] args) {
new Authentication(); }
}
Output:-
Q.2)Write a program using JTextField to perform the addition of two numbers.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdditionOfTwoNum extends JFrame implements ActionListener {
JButton button;
JTextField jtf1, jtf2, jtf3;
JLabel j1, j2;
public AdditionOfTwoNum() {
setVisible(true);
setTitle("Addition Frame");
setSize(300, 300);
dd
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1 = new JLabel("First Number:");
j2 = new JLabel("Second Number:");
jtf1 = new JTextField(15);
jtf2 = new JTextField(15);
jtf3 = new JTextField(15);
jtf3.setEditable(false);
button = new JButton("Addition");
add(j1);
add(jtf1);
add(j2);
add(jtf2);
add(button);
add(jtf3);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {
int n1 = Integer.parseInt(jtf1.getText());
int n2 = Integer.parseInt(jtf2.getText());
int n3 = n1 + n2;
jtf3.setText(Integer.toString(n3));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter valid numbers");
}
}
public static void main(String[] args) {
new AdditionOfTwoNum();
}
}
Output:
dd
Q.3) Write a java program using JPasswordField l to accept password from the user if
the length is less than 6 characters, then error message should be displayed."
Password length must be greater than 6 characters”.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PasswordCheck extends JFrame {
private JPasswordField passwordField;
private JLabel messageLabel;
public PasswordCheck() {
setTitle("Password Checker");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
passwordField = new JPasswordField(20);
messageLabel = new JLabel("Enter your password:");
JButton checkButton = new JButton("Submit");
checkButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkPassword();
}
});
setLayout(new FlowLayout());
add(messageLabel);
dd
add(passwordField);
add(checkButton);
}
private void checkPassword() {
String password = new String(passwordField.getPassword());
if (password.length() < 6) {
JOptionPane.showMessageDialog(this, "Password length must be greater than 6 characters.", "Error",
JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Password accepted!", "Success",
JOptionPane.INFORMATION_MESSAGE);
}}
public static void main(String[] args) {
PasswordCheck frame = new PasswordCheck();
frame.setVisible(true);
}
}
Output:
dd