So I have tried to turn my JLabel
with an Image to a clickable button. But I ran into some problems.
Here's what I used:
Icon image = new ImageIcon("Image Path");
JLabel button = new JLabel(image);
button.setBounds(250, 100, 128, 64); //used a 500x200 window
frame.add(button);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.print("Hello");
}
});
So when I run the code I can click anywhere and I always get the Hello
message.
But I only want the message to be printed out only when I click the image and not anywhere else.
How can I do this?
Thanks!
**Edit: Heres what I really mean:
As you can see, there should be a blue box saying Hello
.
and when I click it, I do get the message Hello
but when I click outside the blue box(the white spots) I still get the message. Is there a way that I can disable that? So that when I only click the blue spot I get the message?**