Advanced Java Programming Practical 13

Question 1
Write a program to demonstrate the use of WindowAdapter class
WindowDemo.java
Java
				import java.awt.*;
import java.awt.event.*;

public class WindowDemo extends Frame
{
    Label l;
    WindowDemo()
    {
        l=new Label();
        l.setBounds(50,50,100,50);
        add(l);
        addWindowListener(new WindowAdapter() {
            public void windowOpened(WindowEvent e)
            {
                l.setText("Window Opened");
            }

            public void windowClosing(WindowEvent e) {
                l.setText("Window Closing");
            }

            public void windowClosed(WindowEvent e) {
                l.setText("Window Closed");
            }

            public void windowActivated(WindowEvent e) {
                l.setText("Window Activated");
            }

            public void windowDeactivated(WindowEvent e)
            {
                l.setText("Window Deactivated");
            }
        });
        setLayout(null);
        setTitle("Window Adapter Demo");
        setSize(500,300);
        setVisible(true);
    }
    public static void main(String args[])
    {
        WindowDemo wd=new WindowDemo();
    }
}

			
Output
Question 2
Write a program to demonstrate the use of anonymous inner class
InnerDemo.java
Java
				interface Student
{
    int roll_no=65;
    void show();
}

public class InnerDemo
{
    public static void main(String args[])
    {
        Student s=new Student() {
            public void show()
            {
                System.out.println("Student roll number is: "+roll_no);
            }
        };
        s.show();
    }
}

			
Output
Question 3
Write a program using MouseMotionAdapter class to implement only one method mouseDragged().
MouseDraggedDemo.java
Java
				import java.awt.*;
import java.awt.event.*;

public class MouseDraggedDemo extends Frame
{
    Label l;
    MouseDraggedDemo()
    {
        l=new Label();
        l.setBounds(50,50,100,50);
        add(l);
        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                l.setText("Mouse Dragged");
            }
        });
        setLayout(null);
        setTitle("Mouse Adapter Demo");
        setSize(500,300);
        setVisible(true);
    }
    public static void main(String args[])
    {
        MouseDraggedDemo md=new MouseDraggedDemo();
    }
}

			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *