图像中的Java鼠标事件侦听器

图像中的Java鼠标事件侦听器,java,event-handling,mouseevent,Java,Event Handling,Mouseevent,是否可以将鼠标侦听器添加到用于游戏开发的图像中。 或者对于任何将图像放入JPanel的类。您可以使用带有缓冲区的JButton,因为这里有一个标准的侦听器供JButton使用 样本: JButton button; BufferedImage buttonIcon; JFrame frame = new JFrame(); try { buttonIcon = ImageIO.read(new File("path")); //path of image button = new

是否可以将鼠标侦听器添加到用于游戏开发的图像中。
或者对于任何将图像放入JPanel的类。

您可以使用带有缓冲区的
JButton
,因为这里有一个标准的侦听器供
JButton
使用

样本:

JButton button;
BufferedImage buttonIcon;
JFrame frame = new JFrame();

try {
   buttonIcon = ImageIO.read(new File("path")); //path of image
   button = new JButton(new ImageIcon(buttonIcon));
} catch (IOException e) {   
   button = new JButton(); //couldn't load Image
}

frame.getContentPane().add(button);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);  


将事件添加到panelWelcome to stack overflow:-)请查看以及如何创建事件。这将有助于获得有用的答案。那么,请告诉我如何在JPanel上指定标签的位置?