0% found this document useful (0 votes)
2 views6 pages

Java pr13

The document contains three Java examples demonstrating the use of AWT (Abstract Window Toolkit) components and event handling. The first example shows a Frame with a WindowAdapter to handle window closing events, the second uses an anonymous inner class for button click actions, and the third illustrates mouse dragging with a MouseMotionAdapter. Each example creates a simple GUI application that responds to user interactions.

Uploaded by

Samruddhi Pise
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)
2 views6 pages

Java pr13

The document contains three Java examples demonstrating the use of AWT (Abstract Window Toolkit) components and event handling. The first example shows a Frame with a WindowAdapter to handle window closing events, the second uses an anonymous inner class for button click actions, and the third illustrates mouse dragging with a MouseMotionAdapter. Each example creates a simple GUI application that responds to user interactions.

Uploaded by

Samruddhi Pise
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/ 6

import java.awt.

*;

import java.awt.event.*;

public class WindowAdapterExample extends Frame {

public WindowAdapterExample() {

// Set the title of the Frame

setTitle("Window Adapter Example");

// Set the size of the Frame

setSize(400, 300);

// Add a WindowAdapter to handle window events

addWindowListener(new WindowAdapter() {

// Override the windowClosing method to handle the close event

public void windowClosing(WindowEvent we) {

// Display a message when the window is about to close

System.out.println("Window is closing...");

// Close the window and terminate the program

System.exit(0);

});

// Set the layout and add a label for demonstration

setLayout(new FlowLayout());

Label label = new Label("Close the window to trigger the WindowAdapter.");

add(label);

// Make the Frame visible

setVisible(true);
}

public static void main(String[] args) {

// Create an instance of the WindowAdapterExample class

new WindowAdapterExample();

import java.awt.*;

import java.awt.event.*;

public class AnonymousInnerClassExample {

public static void main(String[] args) {

// Create a Frame

Frame frame = new Frame("Anonymous Inner Class Example");

// Set frame layout and size


frame.setLayout(new FlowLayout());

frame.setSize(300, 200);

// Create a Button

Button button = new Button("Click Me!");

// Add ActionListener to the button using an anonymous inner class

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// Code to handle button click

System.out.println("Button clicked!");

});

// Add button to the frame

frame.add(button);

// Add a WindowListener using an anonymous inner class to handle window closing

frame.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.out.println("Window closing...");

System.exit(0); // Close the application

});

// Make the frame visible

frame.setVisible(true);

}
import java.awt.*;

import java.awt.event.*;

public class MouseMotionAdapterExample extends Frame {

// Label to display coordinates

Label coordinatesLabel;

public MouseMotionAdapterExample() {

// Set the title of the Frame

setTitle("Mouse Drag Example");

// Set the size of the Frame


setSize(400, 300);

// Set layout to FlowLayout

setLayout(new FlowLayout());

// Create a label to display the mouse coordinates

coordinatesLabel = new Label("Drag the mouse to see the coordinates");

add(coordinatesLabel);

// Add MouseMotionAdapter to handle the mouseDragged event

addMouseMotionListener(new MouseMotionAdapter() {

// Override the mouseDragged method

@Override

public void mouseDragged(MouseEvent e) {

// Get the X and Y coordinates of the mouse drag event

int x = e.getX();

int y = e.getY();

// Display the coordinates in the label

coordinatesLabel.setText("Mouse dragged at X: " + x + ", Y: " + y);

});

// Add WindowListener to handle window closing

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0); // Close the window

});

// Make the frame visible


setVisible(true);

public static void main(String[] args) {

// Create an instance of MouseMotionAdapterExample class

new MouseMotionAdapterExample();

You might also like