|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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... |
| Sponsored Links |
|
|
|
|||||
|
Use the getSource() method.
Java Code:
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
but why if i click in the image1 in this code the message didn't appear and the event didn't work??
Code:
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
|
|
|||||
|
Not sure at the moment, but you may need to implement MouseListener
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||||
|
You added a mouse listener to image1.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
i didn't understand what did you mean??
![]() please explain more.. why if i click in image1 in this code the message didn't appear and the event didn't work?? Last edited by stack; 09-11-2007 at 08:51 AM. |
|
|||||
|
I meant to say - You never added a mouse listener to image1.
Simple doing a Java Code:
Java Code:
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to make image straight - Photoshop | ahsan16 | Photoshop Tutorials | 56 | 07-30-2008 04:11 PM |
| Resize Images And Maintain Original Sharpness | AfTriX | Photoshop Tutorials | 7 | 04-20-2007 10:55 AM |
| PHP Image | NeedHelp | JavaScript and CSS | 1 | 04-20-2007 10:00 AM |
| Resize an Image | Blaze | C# Programming | 3 | 09-22-2006 07:58 PM |
| Question about Form's On_Closing event | hoser2001 | C# Programming | 3 | 08-22-2006 08:25 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |