[英]Add MouseListener to java.awt.Image
Is there a way I can add a MouseListener to an Image? 有什么方法可以将MouseListener添加到图像吗? I took a look at this StackOverflow question, but it didn't really answer how to do this for a
java.awt.Image
, as there is no Graphics2D Image class. 我看了一下这个 StackOverflow问题,但是它没有真正回答如何为
java.awt.Image
做到这一点,因为没有Graphics2D Image类。 Or at least one I've found. 或至少我找到了一个。
EDIT: 编辑:
To clarify, let me try to explain: 为了澄清,让我尝试解释:
With a Ellipse2D
, I can say: 使用
Ellipse2D
,我可以说:
if(ellipse2D.contains(mouseX, mouseY) {
...do something
}
Is this possible with a java.awt.Image
(ie image.contains()
) java.awt.Image
(即image.contains()
)是否可能
Also, this is how the Image gets added to the JPanel: 另外,这是将图像添加到JPanel的方法:
Image image = item.getIcon().getImage(); //item.getIcon() returns a javax.swing.ImageIcon
g.drawImage(image, imageX, imageY, null);
You can just put the image in a JLabel and add the listener to the JLabel 您可以将图像放入JLabel并将侦听器添加到JLabel
ImageIcon image = item.getIcon();
JLabel labelWithImage = new JLabel(image);
labelWithImage.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered Over Image");
}
});
panel.add(labelWithImage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.