在java中为图像或图像添加鼠标监听器

如何在Java中向image或imageIcon添加鼠标侦听器?

这是我的代码。 我想在鼠标点击后对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; } } } 

如果您不想使用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 } }); 

你可以做的是你可以使用JLabel并将ImageIcon设置为它。 使用setOpaque(false)使JLabel透明。 然后,添加监听器并做任何你想做的事:)

最简单的解决方案是使用JLabel并设置它的icon属性。 有关详细信息,请参见如何使用标签 。

如果您必须自己绘制图像(即,您想要在容器中应用效果或执行动画),则需要将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); } } } } 

有关更多详细信息,请参阅执行自定义绘图和如何使用鼠标侦听器