JAVA_PRACTICAL13

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Name:- Priyanka Khandu Thadke

Class:-A(CO5I) Roll no:- 60


Practical no:-13 write a program to demostrate the use of windowAdapter class.
Program code:-
Debug the following code nd erite the output of following 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();

}
}
OutPut:-

Exercise:-
Q.1) write a program to demostrate the use of windowAdapter class.

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:-

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

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);

}
}
Output:-
Q.3)write a java program using MouseMotionAdapter class to implement only oe method
mouseDragged().

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();
}

});

@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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy