0% found this document useful (0 votes)
18 views5 pages

AJP13

The document contains Java code examples demonstrating the use of WindowAdapter, anonymous inner classes, and MouseMotionAdapter classes. Each section includes a code snippet followed by the expected output when the program is executed. The examples illustrate how to handle window closing events and mouse dragging events in a graphical user interface.

Uploaded by

Sumedh Raut
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)
18 views5 pages

AJP13

The document contains Java code examples demonstrating the use of WindowAdapter, anonymous inner classes, and MouseMotionAdapter classes. Each section includes a code snippet followed by the expected output when the program is executed. The examples illustrate how to handle window closing events and mouse dragging events in a graphical user interface.

Uploaded by

Sumedh Raut
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
You are on page 1/ 5

dd

NAME: SUMEDH SANJAY RAUT


CLASS: CO5I(A)
ROLL NO: 532
SUBJECT: AJP(22517)

*PRATICAL NO :- 13

* Debug the following code entired the output of following code.


Code:
import java.awt.*;
import java.awt.event.*;
public class WindowDemo
{
Frame f;
WindowDemo()
{
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.dispose();
}
});
f.setSize(400,400);
f.setVisible(true);
f.setLayout(null);
}
public static void main(String[] args)
{
new WindowDemo();
}
}
dd

Output:-

Exercise:-
Q.1) write a program to demostrate the use of windowAdapter class.
Code:-
import java.awt.*;
import java.awt.event.*;
public class WindowAdapterDemo extends Frame {
public WindowAdapterDemo() {
setTitle("WindowAdapter Example");
setSize(400, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.out.println("Window is closing...");
dispose();
}
});
setLayout(new FlowLayout());
Label label = new Label("Close the window to trigger WindowAdapter.");
add(label);
}
public static void main(String[] args) {
WindowAdapterDemo window = new WindowAdapterDemo();
window.setVisible(true);
}
}
Output:-
dd

Q.2) write a program to demostrate the use of anonymous inner class.


Code:
import java.awt.*;
import java.awt.event.*;
public class AnonymousInnerClassDemo extends Frame {
public AnonymousInnerClassDemo() {
setTitle("Anonymous Inner Class Example");
setSize(300, 200);
setLayout(new FlowLayout());
Button button = new Button("Click Me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!");
}
});
add(button);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.out.println("Window is closing...");
dispose();
}
});
}
public static void main(String[] args) {
AnonymousInnerClassDemo demo = new AnonymousInnerClassDemo();
demo.setVisible(true);
}
}
dd

Output:

Q.3)write a java program using MouseMotionAdapter class to implement only oe


method mouseDragged().
Code:
import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterDemo extends Frame {
private int x = 0;
private int y = 0;
public MouseMotionAdapterDemo() {
setTitle("MouseMotionAdapter Example");
setSize(400, 400);
setBackground(Color.RED);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
dd

@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x, y, 10, 10);
}
public static void main(String[] args) {
MouseMotionAdapterDemo window = new MouseMotionAdapterDemo();
window.setVisible(true);
}
}
Output:

You might also like