Thursday, February 26, 2015

JAVA awt Event Handling:Mouse Event Example

Event Handling:

MouseEvent Example:

------------------------------

This program is the example of MouseEvent .
*************************************************

 
import java.awt.event.*;
import java.awt.*;
class MouseListenerImpl implements MouseListener
{
    public void mouseClicked(MouseEvent me)
    {
        System.out.println("Mouse Clicked.(x="+me.getX()+",y="+me.getY()+")");
        //MyFrame.print(me.getX(),me.getY());
    }
    public void mouseReleased(MouseEvent me)
    {
        System.out.println("Mouse Released.(x="+me.getX()+",y="+me.getY()+")");
    }
    public void mousePressed(MouseEvent me)
    {
        System.out.println("Mouse Pressed.(x="+me.getX()+",y="+me.getY()+")");
    }
    public void mouseEntered(MouseEvent me)
    {
        System.out.println("Mouse Entered.(x="+me.getX()+",y="+me.getY()+")");
    }
    public void mouseExited(MouseEvent me)
    {
        System.out.println("Mouse Exited.(x="+me.getX()+",y="+me.getY()+")");
    }
}
class MyFrame1
{
    public static class MyFrame  extends Frame
    {
    Panel p =new Panel();
    Label x = new Label("X:");
    Label y = new Label("Y:");
    static Label xText = new Label();
    static Label yText = new Label();
    MyFrame()
    {
        this.setVisible(true);
        this.setTitle("Key Event Example");
        this.setBackground(Color.yellow);
        this.setSize(500,500);
        this.setLocationRelativeTo(null);
        this.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
        this.addMouseListener(new MouseListenerImpl());
        this.add(p);
        p.setLayout(new GridLayout(4,2));
//        p.setBackground(Color.gray);
        p.add(x);
        p.add(y);
        p.add(xText);
        p.add(yText);
    }
    public static void print(int x,int y)
    {
        String xString=x+"";
        String yString=y+"";
        xText.setText(xString);
        xText.setText(yString);
//        this.setSize(500,500);
    }
    }
    MyFrame mf = new MyFrame();
}
class  MouseEventEx
{
    public static void main(String[] args)
    {
        MyFrame1 mf = new MyFrame1();
    }
}



Screen Shot:
---------------

 

No comments:

Post a Comment