在java中向图像或图像图标添加鼠标侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21864357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a mouse listener to image or image Icon in java
提问by Ashot
How can I to add mouse listener to image or imageIcon in Java?
如何在 Java 中向 image 或 imageIcon 添加鼠标侦听器?
Here is my code. I want to do anything with imageIcon after mouse click on it.
这是我的代码。鼠标单击后,我想对 imageIcon 执行任何操作。
public class Bubbles extends JPanel {
public static final int LINE_OUT = 440;
int x = 20;
int y = 50;
int v = 4;
Image img = new ImageIcon("res/lop.png").getImage();
BackSide back;
public Bubbles(int x, int y, int v, BackSide back) {
this.x = x;
this.y = y;
this.v = v;
this.back = back;
setFocusable(true);
}
public void move() {
if (y >= 440) {
y = 0;
} else {
y += v;
}
}
}
采纳答案by Tomas Bisciak
If you dont want to use JLabel you can paint the image onto panel by overriding paintComponent method.Then you can add listener to your JPanel.I woud strongly recomend you to use JLabel but if you still preffer the other way,here you can see some example how to do it. How to set background image in Java?.
如果您不想使用 JLabel,您可以通过覆盖paintComponent 方法将图像绘制到面板上。然后您可以向您的 JPanel 添加侦听器。我强烈建议您使用 JLabel 但如果您仍然喜欢其他方式,在这里您可以看到一些例如如何做到这一点。如何在Java中设置背景图像?.
Icon icon = new ImageIcon("give path of the image");
JLabel lb = new JLabel(icon);
//now set listener for label.
lb.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
//statement
}
});
回答by Little Child
What you can do is that you can use a JLabel
and set the ImageIcon
to it. make the JLabel
transparent by using setOpaque(false)
. Then, add listener to it and do whatever you want to do :)
您可以做的是,您可以使用 aJLabel
并将其设置ImageIcon
为它。JLabel
通过使用使透明setOpaque(false)
。然后,向它添加侦听器并执行您想做的任何操作:)
回答by MadProgrammer
The simplest solution would be to use a JLabel
and set it's icon
property. See How to use labelsfor more details.
最简单的解决方案是使用 aJLabel
并设置它的icon
属性。有关更多详细信息,请参阅如何使用标签。
If you must paint the image yourself (ie, you want to apply effects or perform animation across a container), then you need to add the MouseListener
to the container which is rendering the image and test to see if the mouse event occurred within the context of the image based on it's location and size.
如果你必须自己粉刷(即,要应用效果或进行跨容器动画)的图像,然后你需要添加MouseListener
到这是在渲染图像和测试,看看如果鼠标事件的背景下发生的容器图像基于它的位置和大小。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ImageMouseListener {
public static void main(String[] args) {
new ImageMouseListener();
}
public ImageMouseListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage img;
private Point imgPoint;
public ImagePane() {
try {
img = ImageIO.read(...);
imgPoint = new Point(100, 100);
} catch (IOException ex) {
ex.printStackTrace();
}
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (img != null && imgPoint != null) {
Point me = e.getPoint();
Rectangle bounds = new Rectangle(imgPoint, new Dimension(img.getWidth(), img.getHeight()));
if (bounds.contains(me)) {
System.out.println("I was clicked!");
}
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null && imgPoint != null) {
g.drawImage(img, imgPoint.x, imgPoint.y, this);
}
}
}
}
Take a look at Performing Custom Paintingand How to use Mouse Listenersfor more details