Ayush Practical No 11

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ADVANCE JAVA PROGRAMMING

Name :- Ayush Maroti Wadje Class : - CO5I


Roll No :- 76

Pra No 11 :- Write a Program To Demonstrate Various Mouse Events Using


MouseListener And MouseMotion Listener Interface.

X. Program Code.
1. Debug The Following Program Code And Write The Output.

Code :-

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class MouseDemo extends Applet implements MouseListener {

Label l;

public void init()

setLayout(null);

l=new Label("Hello Mouse");

l.setBounds(50,150,200,100);

add(l);

1
}

public void mousePressed(MouseEvent e)

l.setText("Mouse Pressed no. of clicks:"+e.getClickCount()+"at


position"+e.getX()+","+e.getY());

public void mouseReleased(MouseEvent e)

l.setText("Mouse Released; # of clicks."+e.getClickCount());

public void mouseEntered(MouseEvent e)

l.setText("Mouse Entered");

public void mouseExited(MouseEvent e)

l.setText("Mouse Exited");

public void mouseClicked(MouseEvent e)

l.setText("mouse clicked(# of clicks:"+e.getClickCount());

2
Output :-

XIII. Exercise.

1. Write a Program To Change The Background Color Of Applet When User


Performs Events Using Mouse.
2. Code :-
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MouseExample1 extends JFrame implements
MouseListener{
TextArea t=new TextArea("Click to change the Backgound
color");
MouseExample1()
{

setLayout(new FlowLayout());
setSize(600,600);
setVisible(true);

3
addMouseListener(this);
add(t);
}
public static void main(String args[])
{ new MouseExample1();
}

@Override
public void mouseClicked(MouseEvent e) {

@Override
public void mousePressed(MouseEvent e) {

@Override
public void mouseReleased(MouseEvent e) {

@Override
public void mouseEntered(MouseEvent e) {

@Override
public void mouseExited(MouseEvent e) {

}
}

4
Output :-

3. Write a Program To Count The Number Of Clicks Performed By The User


In a Frame Window.
Code :-

import java.awt.*;
import java.awt.event.*;
public class MouseListenerEx1 extends Frame implements
MouseListener{
Label L;
MouseListenerEx1(){
addMouseListener(this);

5
L=new Label();
L.setBounds(25,55,130,25);
add(L);
setSize(300,350);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
L.setText("Mouse Clicked "+e.getClickCount());
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String[] args) {
new MouseListenerEx1();
}
}

Output :-

6
4. Write a Program To Demonstrate The Use Of MouseDragged And
MouseMoved Method Of MouseMotionListener.
Code :-

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MouseMotionExample2 extends JFrame
implements MouseMotionListener{
JTextArea T1=new JTextArea("Move your mouse here
",35,25);
JTextArea T2=new JTextArea("Drag your mouse
here",35,25);
MouseMotionExample2()
{
setLayout(new FlowLayout());

7
setSize(600,600);
setVisible(true);
addMouseMotionListener(this);
add(T1);
add(T2);
}
public void mouseMoved(MouseEvent e)
{
T1.setText("Mouse is Moving\n Position Of Mouse Is At :
"+ e.getX()+" "+e.getY());
}

public void mouseDragged(MouseEvent e)


{
T2.setText("Mouse is Dragging\n Position Of Mouse Is
At : "+ e.getX()+" "+e.getY());
}
public static void main(String args[])
{
new MouseMotionExample2();
}
}

8
Output :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy