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

Practical No. 13[1]

The document outlines a laboratory exercise for a 5th semester Advance Java Programming course, focusing on the use of various Java classes such as WindowAdapter, ActionListener, and MouseMotionAdapter. It includes code examples demonstrating how to handle window events, button clicks, and mouse drag events. The exercises aim to enhance students' understanding of event handling in Java GUI applications.

Uploaded by

letscookbrba
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)
23 views

Practical No. 13[1]

The document outlines a laboratory exercise for a 5th semester Advance Java Programming course, focusing on the use of various Java classes such as WindowAdapter, ActionListener, and MouseMotionAdapter. It includes code examples demonstrating how to handle window events, button clicks, and mouse drag events. The exercises aim to enhance students' understanding of event handling in Java GUI applications.

Uploaded by

letscookbrba
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/ 5

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Advance Java Programming Subject Code:22517


Semester:5th Semester Course: Computer Engineering
Laboratory No: L003 Name of Subject Teacher: Prof. Prasad Koyande
Name of Student: Parth Bagwe Roll Id: 22203A0019

Experiment No: 13
Title of Experiment Write a program to demonstrate the use of WindowAdapter class

Practice Set:

1. Write a program to demonstrate the use of Window Adapter class.


Answer: -
import java.awt.*;
import java.awt.event.*;

public class WindowAdapterDemo extends Frame {


public WindowAdapterDemo() {
// Set frame title
setTitle("WindowAdapter Demo");

// Set frame size


setSize(400, 300);

// Add WindowAdapter to handle window events


addWindowListener(new WindowAdapter() {
// Handle window closing
public void windowClosing(WindowEvent we) {
System.out.println("Window is closing");
dispose(); // Close the window
}

Page | 1
// Handle window opening
public void windowOpened(WindowEvent we) {
System.out.println("Window has opened");
}

// Handle window minimizing (iconified)


public void windowIconified(WindowEvent we) {
System.out.println("Window is minimized");
}

// Handle window restoring from minimized state (deiconified)


public void windowDeiconified(WindowEvent we) {
System.out.println("Window is restored from minimized state");
}
});
}

public static void main(String[] args) {


WindowAdapterDemo frame = new WindowAdapterDemo();
frame.setVisible(true);
}
}
Output: -

Page | 2
2. Write a program to demonstrate the use of anonymous inner class.
Answer: -
import java.awt.*;
import java.awt.event.*;

public class AnonymousInnerClassDemo {


public static void main(String[] args) {
Frame frame = new Frame(“Anonymous Inner Class Demo”);
Button button = new Button(“Click Me!”);

// Add anonymous inner class to handle button click event


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(“Button clicked!”);
}
});

frame.add(button);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);

// Add a window closing event to exit the application


frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);

Page | 3
}
});
}
}
Output: -

3. Write a program using MouseMotionAdapter class to implement only


one method mouseDragged().
Answer: -

import java.awt.*;
import java.awt.event.*;

public class MouseMotionAdapterDemo extends Frame {

Page | 4
public MouseMotionAdapterDemo() {
setTitle("MouseMotionAdapter Demo");
setSize(400, 300);

// Add MouseMotionAdapter to handle mouse dragged event


addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
// Display the coordinates when the mouse is dragged
setTitle("Mouse Dragged at (" + me.getX() + ", " + me.getY() +
")");
}
});

// Add window closing event


addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}

public static void main(String[] args) {


MouseMotionAdapterDemo frame = new
MouseMotionAdapterDemo();
frame.setVisible(true);
}
}
Output: -

Page | 5

You might also like