hi,
i have a question:ex: if i have 4 images (image1, image2, image3, image4) and i want message1 appear if i click in image1, message2 appear if i click in image2, message3 appear if i click in image3, and message4 appear if i click in image4
how can I know if the mouse event in image1 or 2 or 3 or 4 without using the position of these images in the board?
i want a method to tell me the event in which image...
question to know the event in which image?
Started by stack, Sep 02 2007 02:15 AM
10 replies to this topic
#1
Posted 02 September 2007 - 02:15 AM
|
|
|
#2
Posted 04 September 2007 - 09:46 AM
Use the getSource() method.
[highlight="Java"]public void actionPerformed(ActionEvent e){
if(e.getSource().equals(image1)) {
//show message 1
} else if(e.getSource().equals(image2)) {
//show message 2
}
[/highlight]
[highlight="Java"]public void actionPerformed(ActionEvent e){
if(e.getSource().equals(image1)) {
//show message 1
} else if(e.getSource().equals(image2)) {
//show message 2
}
[/highlight]
#3
Posted 04 September 2007 - 01:18 PM
thank you,, and i will try it
#4
Posted 05 September 2007 - 04:33 AM
but why if i click in the image1 in this code the message didn't appear and the event didn't work??
import javax.swing.JApplet;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class SS extends JApplet
{
private ImageIcon image1;
public void init(){
image1 = new ImageIcon("instruction.jpg");
}
public void paint(Graphics g){ //paint
image1.paintIcon(this,g,0,0);
addMouseListener(
new MouseAdapter()
{//MouseAdapter
public void mousePressed(MouseEvent e){ //press
if(e.getSource().equals(image1)) { //msg
System.out.printf("YOU CLICK IN IMAGE1");
JOptionPane.showMessageDialog( null, "GOOD MORNING ",
"HI",JOptionPane.INFORMATION_MESSAGE );
} //msg
}//press
}//MouseAdapter
);//addMouseListener
} //paint
}//SS
#5
Posted 06 September 2007 - 01:29 AM
:confused:
#6
Posted 06 September 2007 - 03:52 AM
#7
Posted 09 September 2007 - 02:03 PM
can you try my code please???
#8
Posted 10 September 2007 - 07:29 AM
#9
Posted 11 September 2007 - 12:52 AM
Sidewinder said:
You added a mouse listener to image1.
i didn't understand what did you mean??:confused:
please explain more..
why if i click in image1 in this code the message didn't appear and the event didn't work??
#10
Posted 11 September 2007 - 07:26 AM
I meant to say - You never added a mouse listener to image1.
Simple doing a
[highlight=Java]addMouseListener()[/highlight]
Adds the mouse listener to the root content panel. You have to add the mouse listener to the object you want it to listen to a la
[highlight=Java]image1.addMouseListener()[/highlight]
Simple doing a
[highlight=Java]addMouseListener()[/highlight]
Adds the mouse listener to the root content panel. You have to add the mouse listener to the object you want it to listen to a la
[highlight=Java]image1.addMouseListener()[/highlight]
#11
Posted 11 September 2007 - 12:25 PM
Sidewinder said:
I meant to say - You never added a mouse listener to image1.
Simple doing a
[highlight=Java]addMouseListener()[/highlight]
Adds the mouse listener to the root content panel. You have to add the mouse listener to the object you want it to listen to a la
[highlight=Java]image1.addMouseListener()[/highlight]
Simple doing a
[highlight=Java]addMouseListener()[/highlight]
Adds the mouse listener to the root content panel. You have to add the mouse listener to the object you want it to listen to a la
[highlight=Java]image1.addMouseListener()[/highlight]
the event didn't work with me..and this error appear during compile:
SS.java:21: cannot find symbol
symbol : method addMouseListener(<anonymous java.awt.event.MouseAdapter>)
location: class javax.swing.ImageIcon
image1.addMouseListener(
^
1 error


Sign In
Create Account


Back to top









