| ★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Step/Chapter2/MouseClickExample.html | Nouvelle recherche | Portail wanayoo |
![]() | |||||||||||||||||||||
Tutorials : Step by Step :
|
|||||||||||||||||||||
| Contents |
| Event Listeners |
| The ActionListener |
| The MouseListener |
| The MouseMotionListener |
|
/* You might want to create
areas where the user can click instead
import java.awt.*; // Tells the applet you will be using the MouseListener methods.
public class MouseClickExample
extends Applet implements MouseListener
// The coordinates of the
rectangle we will draw.
// The variable that will tell
whether or not the mouse
// variable that will be true
when the user clicked in the rectangle
public void
init()
// Add the MouseListener to
your applet
public void paint(Graphics g) g.fillRect(rect1xco,rect1yco,rect1width,rect1height); g.setColor(Color.red);
// When the user clicks this
will show the coordinates of the click
// If the click was in the
rectangle show this message
if (mouseEntered) g.drawString("Mouse is in the applet
area",20,160); /* These methods always have to be present when you implement MouseListener
public void mouseClicked
(MouseEvent me) {}
// This method will be called
when the mouse has been clicked.
// Save the coordinates of the
click like this.
// Check if the click was
inside the rectangle area. }
// This is called when the mouse
has been pressed
// When it has been
released
// This is executed when the
mouse enters the applet. it will only
// When the Mouse leaves the
applet.
/* So now you can use the
MouseListener instead of Buttons. These methods will be
ones that you will }
|
|
Applet Index (sorted alphabetically) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #s |
The Java Source (applets w/source code) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
New Article:
J2EE Deployment Specification
Why is it that J2EE
applications written in the same language, Java, may not coexist
on different servers? While the answer isn't simple Sun has set
out to alleviate these problems with the "J2EE Deployment
Specification".
... [more articles]
New Tutorial:
Designing Packages for Stability
Are your designs stable? This month Samudra will focus on the change impact relationship involved in designing classes showing us how to take them into account when designing the package relationship.
... [more Tutorials]
Year in Review:
The Best of 2002
The year 2002 saw 201 new applets posted to JavaBoutique.
Check out the 2 most popular apps from each month of 2002!
... [popular applets]
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
|
