Apr 20, 2012

Adapter Class


Java provides adapter classes that implement the corresponding listener interface containing more than one method. The methods in the classes are empty. The listener class that you define can extend the Adapter class and override the methods that you need.

Example:


import java.awt.*;
import java.awt.event.*;

public class AdapterDemo
{
    public static void main(String[] args)
    {
        AdapterDemo1 d=new AdapterDemo1();
        d.setBounds(100,100,300,200);
        d.show();
    }
}
class AdapterDemo1 extends Frame
{
    public AdapterDemo1()
    {
        setTitle("Click close button to Quit");
        setSize(300, 200);
        setBackground(Color.gray);
        addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                });
    }
}

Output:







0 comments :

Post a Comment