MouseEvent

Advertisements

Prev Tutorial Next Tutorial

Event Handling for Mouse

For handling event for mouse you need MouseEvent class and MouseListener interface.

GUI ComponentEvent classListener Interface
MouseMouseEventMouseListener

Method (abstract method)

mousePressed(MouseEvent e): This method will be execute whenever mouse button is pressed (not released).

mouseReleased(MouseEvent e): This method will be execute whenever mouse button is only released (if already it is pressed).

mouseClicked(MouseEvent e): This method will be execute whenever mouse button is both pressed and released.

mouseEntered(MouseEvent e): This method will be execute whenever mouse cursor position is placed on specific location or component.

mouseExited(MouseEvent e): This method will be execute whenever mouse cursor position is taken back from any location or component.

Example MouseEvent

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

class MouseEventEx extends Frame implements MouseListener 
{
int x=100,y=200;
int count=0;

MouseEventEx()
{
addMouseListener(this);
}
	
public void paint(Graphics g)
{
count++;
if(count<=3)
{
g.setColor(Color.cyan);
}
else
{
g.setColor(Color.red);
}
setFont(new Font("TimesRoman",Font.BOLD,30));
g.drawString("Hello",x,y);
}

public void mouseClicked(MouseEvent e)
{
x=e.getX();
y=e.getY();
repaint();
}
public void mousePresed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
	
public static void main(String[] args) 
{
MouseEventEx om=new MouseEventEx();
Toolkit tk=Toolkit.getDefaultToolkit();
om.setSize(tk.getScreenSize());
om.setBackground(Color.cyan);
om.setVisible(true);		
}
}

Download code Click

mouseevent example
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements