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