Program:
Output:
2) Write a program to demonstrate the use of anonymous inner class.
Ans:
Output:

3) Write a program using MouseMotionAdapter class to implement only one method mouseDragged().
Ans:
Output:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.WindowListener;
import java.awt.FlowLayout;
public class WindowAdapterDemo extends WindowAdapter
{
JFrame f ;
JLabel l ;
WindowAdapterDemo()
{
f = new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());
f.addWindowListener(this);
f.addWindowFocusListener(this);
}
public void windowLostFocus(WindowEvent we)
{
l = new JLabel("Window Lost Focus");
f.remove(l);
f.add(l);
}
public void windowOpened(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Opened");
f.remove(l);
f.add(l);
}
public void windowActivated(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Activated");
f.remove(l);
f.add(l);
}
public void windowDeactivated(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Deactivated");
f.remove(l);
f.add(l);
}
public void windowGainedFocus(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Gained Focus");
f.remove(l);
f.add(l);
}
public static void main(String[] args)
{
WindowAdapterDemo wa = new WindowAdapterDemo();
}
}
Output:
2) Write a program to demonstrate the use of anonymous inner class.
Ans:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class AnonymousInner extends JFrame
{
public static void main(String[] args) {
AnonymousInner ai = new AnonymousInner();
JButton jb = new JButton(" Don't Click Me Bro ");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println("Action Event:"+ae);
}
});
ai.add(jb);
ai.pack();
ai.setVisible(true);
}
}
Output:

3) Write a program using MouseMotionAdapter class to implement only one method mouseDragged().
Ans:
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.applet.Applet;
public class AdapterMouseDrag extends Applet
{
public void init()
{
addMouseMotionListener(new MouseDrag(this));
}
}
class MouseDrag extends MouseMotionAdapter
{
AdapterMouseDrag ad;
public MouseDrag(AdapterMouseDrag ad)
{
this.ad = ad;
}
public void mouseDragged(MouseEvent me)
{
ad.showStatus("Mouse Dragged");
}
}
/*
*/
Output:

