0% found this document useful (0 votes)
14 views4 pages

Practical No 11

The document contains Java code examples demonstrating the use of mouse event listeners in GUI applications. It includes a MouseListenerDemo class that updates a label based on mouse actions, and two additional examples showcasing mouse motion handling in an Applet and a Frame. The examples illustrate how to change background colors and display mouse coordinates in response to user interactions.

Uploaded by

os7498097
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Practical No 11

The document contains Java code examples demonstrating the use of mouse event listeners in GUI applications. It includes a MouseListenerDemo class that updates a label based on mouse actions, and two additional examples showcasing mouse motion handling in an Applet and a Frame. The examples illustrate how to change background colors and display mouse coordinates in response to user interactions.

Uploaded by

os7498097
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical no 11

import java.awt.*;
import java.awt.event.*;
public class MouseListenerDemo extends Frame implements MouseListener
{
Label l;
public MouseListenerDemo()
{
l = new Label();
l.setBounds(20,50,100,20);
add(l);
l.addMouseListener(this);
}
public void mouseClicked(MouseEvent me)
{
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent me)
{
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent me)
{
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent me)
{
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent me)
{
l.setText("Mouse Released");
}
public static void main(String arg[])
{
MouseListenerDemo m=new MouseListenerDemo();
m.setSize(500,500);
m.setVisible(true);
m.setTitle("mouse listener Example");
}
}
Exercise

1]
import java.awt.*;

import java.applet.*;
import java.awt.event.*;
public class MouseColor extends Applet implements MouseMotionListener
{
MouseColor(){
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me)
{
setBackground(Color.red);
repaint();
}
public void mouseMoved(MouseEvent me)
{
setBackground(Color.red);
repaint();
}
public static void main(String args[]){
MouseColor mc=new MouseColor ();
mc.setVisible (true);
mc.setSize(200,200);
}
}
/*<applet code="MouseColor" height=350 width=400>
</applet>*/

2]
import java.awt.*;
import java.awt.event.*;
public class MouseMotionDemo1 extends Frame implements MouseMotionListener
{
Label label1;
Color bgColor = Color.BLUE;
public MouseMotionDemo1()
{
label1 = new Label();
label1.setBounds(20, 40, 100, 20);
add(label1);
addMouseMotionListener(this);
setSize(400, 400);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
label1.setText("X=" + e.getX() + ", Y=" + e.getY());
Graphics g = getGraphics();
g.setColor(Color.BLACK);
g.fillOval(e.getX(), e.getY(), 10, 10);
}
public void mouseMoved(MouseEvent e)
{
label1.setText("X=" + e.getX() + ", Y=" + e.getY());
}
public static void main(String[] args)
{ new MouseMotionDemo1();}}

You might also like