Friday, 24 January 2014

Event Handling Mouse Motion Program

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

public class eventDemo extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    Button b = new Button("+");
    Button b2 = new Button("-");
    TextField t1 = new TextField(20);
    TextField t2 = new TextField(20);
    Label ans = new Label("Ans goes here");
    public void init()
    {
        b.addActionListener(this);
        b2.addActionListener(this);
        ans.addMouseListener(this);
        this.addMouseMotionListener(this);
        add(t1);
        add(t2);
        add(b);
        add(b2);
        add(ans);
    }
    public void actionPerformed(ActionEvent e)
    {
        int a = Integer.parseInt(t1.getText());
        int bt = Integer.parseInt(t2.getText());
        if(e.getSource()==b)
        {
            int an = a+bt;
            ans.setText(an+"");
        }
        else if(e.getSource()==b2)
        {
            int an = a-bt;
            ans.setText(an+"");
            
        }
        
    }
    
    public void mouseClicked(MouseEvent e)
    { 
        t1.setText("Clicked");
    }
    public void mouseEntered(MouseEvent e)
    {
        t1.setText("Entered");
    }
    public void mouseExited(MouseEvent e)
    {
        t1.setText("Exited");
    }
    public void mousePressed(MouseEvent e)
    {
        t1.setText("Pressed");
    }
    public void mouseReleased(MouseEvent e)
    {
        t1.setText("Released");
    }
    
    public void mouseMoved(MouseEvent e)
    {
        t2.setText(e.getX() + "," + e.getY());
    }
    public void mouseDragged(MouseEvent e)
    {
        
    }
}

/*<applet code="eventDemo" width="400" height="400"></applet>*/

OUTPUT


0 comments:

Post a Comment

Followers