0% found this document useful (0 votes)
16 views20 pages

Java GUI Event Handling Examples

The document contains a series of practical programming exercises demonstrating various Java and web development concepts, including key and mouse event handling, form validation using regular expressions, and arithmetic expression evaluation in JavaScript. Each practical includes code snippets and aims to illustrate specific functionalities, such as using JTextField and JPasswordField, InetAddress class, and creating a slideshow banner. Additionally, it provides output sections for marking and teacher signatures.

Uploaded by

Soham Bijwar
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)
16 views20 pages

Java GUI Event Handling Examples

The document contains a series of practical programming exercises demonstrating various Java and web development concepts, including key and mouse event handling, form validation using regular expressions, and arithmetic expression evaluation in JavaScript. Each practical includes code snippets and aims to illustrate specific functionalities, such as using JTextField and JPasswordField, InetAddress class, and creating a slideshow banner. Additionally, it provides output sections for marking and teacher signatures.

Uploaded by

Soham Bijwar
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

PRACTICAL NO.

Aim:- Write a program to demonstrate status of key on applet window such as


keyPressed,keyRelesed,keyUp,keyDown.
import [Link].*;

import [Link].*;

public class KeyListenerExample extends Frame implements KeyListener

Label l;

TextArea area;

KeyListenerExample()

l = new Label();

[Link] (20, 50, 100, 20);

area = new TextArea();

[Link] (20, 80, 300, 300);


[Link](this); add(l);

add(area);

setSize (400, 400); setLayout (null);

\ setVisible (true);

public void keyPressed (KeyEvent e)

[Link] ("Key Pressed");

public void keyReleased (KeyEvent e)

[Link] ("Key Released");


}

public void keyTyped (KeyEvent e)

[Link] ("Key Typed");

public sta c void main(String[] args)

new KeyListenerExample();

Output:-

Marks Obtained Dated signature of


Teacher
Process Related(35) Product Related(15) Total(50)
PRACTICAL NO. 9

AIM:-Write a program to demonstrate various mouse events using mouselistener and


mousemo onlistener interface.

Program:
import [Link].*;

import [Link].*;

public class MouseEventDemo extends JFrame implements MouseListener, MouseMo onListener {

private JTextArea textArea;

public MouseEventDemo() {

setTitle("Mouse Event Demo");

setSize(400, 300);

setDefaultCloseOpera on(JFrame.EXIT_ON_CLOSE);

textArea = new JTextArea();

[Link](false);

add(new JScrollPane(textArea));

[Link](this);

[Link] onListener(this):

setVisible(true);

}
@Override

public void mouseClicked(MouseEvent e) {

[Link]("Mouse clicked at (" + [Link]() + ", " + [Link]() + ")\n");

@Override

public void mousePressed(MouseEvent e) {

[Link]("Mouse pressed at (" + [Link]() + ", " + [Link]() + ")\n");

@Override

public void mouseReleased(MouseEvent e) {

[Link]("Mouse released at (" + [Link]() + ", " + [Link]() + ")\n");

@Override

public void mouseEntered(MouseEvent e) {

[Link]("Mouse entered the component\n");

@Override

public void mouseExited(MouseEvent e) {

[Link]("Mouse exited the component\n");

@Override

public void mouseDragged(MouseEvent e) {

[Link]("Mouse dragged at (" + [Link]() + ", " + [Link]() + ")\n");

@Override

public void mouseMoved(MouseEvent e) {

[Link]("Mouse moved at (" + [Link]() + ", " + [Link]() + ")\n");

public sta c void main(String[] args) {


SwingU li [Link](() -> new MouseEventDemo());

Output:

Marks Obtained Dated signature of


Teacher
Process Related(35) Product Related(15) Total(50)
PRACTICAL NO. 10

AIM:-write a program to demonstrate the use of JTex ield and JPasswordField using Listener
Interface
Program:-

import [Link].*;
import [Link] onEvent;
import [Link] onListener;
public class SimpleTextFieldPasswordFieldDemo extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JTextArea outputArea;
private JBu on submitBu on;
public SimpleTextFieldPasswordFieldDemo() {
setTitle("TextField and PasswordField Demo");
setSize(400, 200);
setDefaultCloseOpera on(JFrame.EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JPanel usernamePanel = new JPanel();
[Link](new JLabel("Username:"));
usernameField = new JTextField(20);
[Link](usernameField);
JPanel passwordPanel = new JPanel();
[Link](new JLabel("Password:"));
passwordField = new JPasswordField(20);
[Link](passwordField);
submitBu on = new JBu on("Submit");
submitBu [Link] onListener(new Ac onListener() {
@Override
public void ac onPerformed(Ac onEvent e) {
String username = [Link]();
String password = new String([Link]());
[Link]("Username: " + username + "\nPassword: " + password);
}
});
outputArea = new JTextArea(5, 30);
[Link](false);
JScrollPane scrollPane = new JScrollPane(outputArea);

// Add components to the frame


add(usernamePanel);
add(passwordPanel);
add(submitBu on);
add(scrollPane);
setVisible(true);
}

public sta c void main(String[] args) {


SwingU li [Link](() -> new SimpleTextFieldPasswordFieldDemo());
}
}
OUTPUT:

Marks Obtained Dated signature of


Teacher
Process Related(35) Product Related(15) Total(50)
PRACTICAL NO. 11

Aim:- Write a Pragram to demonstrate the use of InetAddress class and its
factory methods

Program:-
import [Link];
import [Link] on;
public class InetAddressDemo {
public sta c void main(String[] args) {
try {
InetAddress localAddress = [Link]();
[Link]("Local IP Address: " + [Link]());
[Link]("Local Hostname: " + [Link]());
String hostname = "[Link]";
InetAddress hostAddress = [Link](hostname);
[Link]("IP Address of " + hostname + ": " + [Link]());
[Link]("Hostname: " + [Link]());
String domain = "[Link]";
InetAddress[] domainAddresses = [Link](domain);
[Link]("IP Addresses of " + domain + ":");
for (InetAddress address : domainAddresses) {
[Link](" " + [Link]());
}
} catch (UnknownHostExcep on e) {
[Link]("Host could not be resolved: " + [Link]());
}
}
}

Output:

Marks Obtained Dated signature of


Teacher
Process Related(35) Product Related(15) Total(50)
PRACTICAL NO. 9

Alm: Develop a webpage for validation of form fields using regular expressions.

What is regular Expression?

Regular expressions are an important notation for defining patterns. Each pattern connects a set
of strings. Therefore regular expressions will give as names for sets of strings.

It supports an appropriate and useful notation for describing tokens. Regular Expressions define
the language accepted by finite Automata (Transition Diagram).

Regular Expressions are defined over an alphabetΣ


if R is a Regular Expression, therefore L(R) represents language denoted by the regular expression.

Program:-

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

func on checkEmail()

var email = [Link]('email').value;

var regex = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;

var res = [Link](email);

if (!res)

alert("Please enter a valid email address.");

else

{
alert("You entered a correct email address.");

</script>

</head>

<body>

<form name="myform" ac on="#" method="post">

Enter Email ID: <input type="text" id="email" /><br />

<input type="bu on" value="Submit" onclick="checkEmail()" />

</form>

</body>

</html>

Output:-

Marks Obtained Dated signature of


Teacher
Process Related(15) Product Related(10) Total(25)
PRACTICAL NO. 10

Aim:-Develop a webpage for implementa on Minus.

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, ini al-scale=1.0">
< tle>Simple Subtrac on in JavaScript</ tle>
<style>
</style>
</head>
<body>
<div class="container">
<h2>Subtrac on Calculator</h2>
<p>Enter two numbers to subtract:</p>
<label for="num1">Number 1:</label>
<input type="number" id="num1" placeholder="Enter first number">

<label for="num2">Number 2:</label>


<input type="number" id="num2" placeholder="Enter second number">

<bu on onclick="subtract()">Subtract</bu on>

<div class="result" id="result"></div>


</div>
<script>
func on subtract() {

const num1 = parseFloat([Link]("num1").value);


const num2 = parseFloat([Link]("num2").value);

if (isNaN(num1) || isNaN(num2)) {
[Link]("result").innerText = "Please enter valid numbers.";
} else {

const result = num1 - num2;

[Link]("result").innerText = "Result: " + result;


}
}
</script>
</body>
</html>
Output :-

Marks Obtained Dated signature of


Teacher
Process Related(15) Product Related(10) Total(25)
PRACTICAL NO. 11

Aim: Develop a Webpage for Implementing Slideshow banner.

Basic Concept:-
Displaying banners ads is a common practice for showing advertisements on web pages
to the visitors. Banners ads are normally created using standard graphic tools such as
Photoshop, Paintbrush Pro, and other software. Banner ads can be static or animated.
Animated images are animated GIF files or flash movies. Flash movies are created
using Macromedia Flash and the browsers must have installed flash plugin to view the
movies. On the other hand, you can create some animated effect using JavaScript, like
rotating static banner ads at a certain time interval.

Creating Rotating Banner Ads


Rotating banners ads comprises several banner images that constantly rotate on a
webpage at a fix time interval. You can create these banner images using standard
graphics tools. Let's create four banner images and name them as [Link],
[Link], [Link] and [Link]
The JavaScript starts by declaring an array to store the banner images using the new
Array keywords, as follows

MyBanners=new Array('[Link]', '[Link]', '[Link]', '[Link]")


Each element in the array is assigned with an index, starting from 0. In our example,
[Link] is assigned with index 0, [Link] is assigned with index 1,
[Link] is assigned with index 2 and [Link] is assigned with index 3.

Program:-
<!DOCTYPE html>
<html>
<head>
<title>Banner</title>
</head>
<body>
<center>
<a href="[Link]
<img src="[Link]
P_QuizSpot_banner4.jpeg" height="200" width="400">
</a>
</center>
</body>
</html>
Output:-

Marks Obtained Dated signature of


Teacher
Process Related(15) Product Related(10) Total(25)
PRACTICAL NO. 12

Aim:- Write simple javascript with HTML for arithma c expression evalua on
and message prin ng.

Program:-
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, ini al-scale=1.0">
< tle>Arithme c Expression Evalua on</ tle>
<script>
func on evaluateExpression() {
let expression = [Link]("expression").value;
try {
let result = eval(expression);
[Link]("result").innerHTML = "Result: " + result;
} catch (error) {
[Link]("result").innerHTML = "Invalid Expression!";
}
}
</script>
</head>
<body>
<h2>Arithme c Expression Evalua on</h2>
<label for="expression">Enter an arithme c expression:</label><br>
<input type="text" id="expression" placeholder="e.g., 3+5*2"><br><br>
<bu on onclick="evaluateExpression()">Evaluate</bu on>
<p id="result"></p>
</body>
</html>

Output:-

Marks Obtained Dated signature of


Teacher
Process Related(15) Product Related(10) Total(25)

You might also like