简体   繁体   English

MouseListener事件在Java中不起作用

[英]MouseListener event not working in Java

I'm creating an application in which a list of images will be displayed in JList. 我正在创建一个应用程序,其中图像列表将显示在JList中。 Each image will be displayed as embedded in a panel. 每个图像将显示为嵌入在面板中。 When user moves cursor over the images, images should be displayed as enlarged in another frame. 当用户将光标移到图像上时,图像应在另一帧中放大显示。

Now, I've implemented MouseListner interface over each panel displaying an event. 现在,我已经在显示事件的每个面板上实现了MouseListner界面。 and I've added mouseListner() over each panel. 并且我在每个面板上都添加了mouseListner()。

Now, when user clicks or moves in or out of panel .. no events are occuring. 现在,当用户单击或移入或移出面板时,..不会发生任何事件。

Is it because of the fact that when I'm clicking on an image, I'm actually clicking on JLabel and not on the panel directly? 是否是因为当我单击图像时实际上是在单击JLabel而不是直接在面板上?

What might be other cause? 可能是其他原因造成的?

Is it because of the fact that when I'm clicking on an image, I'm actually clicking on JLabel and not on the panel directly? 是否是因为当我单击图像时实际上是在单击JLabel而不是直接在面板上?

You are actually clicking on the JList. 您实际上是在单击JList。 A renderer is NOT a real component. 渲染器不是真正的组件。

You need to add the MouseListener to the JList and then determine which rendered item was clicked by using the indexToLocation(...) method of the JList. 您需要将MouseListener添加到JList,然后通过使用JList的indexToLocation(...)方法确定单击了哪个呈现项目。

Edit: 编辑:

Actually that should be locationToIndex(...) method of the JList. 实际上,这应该是JList的locationToIndex(...)方法。 Sometimes my fingers don't type what my mind is thinking :) 有时我的手指没有输入我的想法:)

如果要捕获鼠标移动事件,则需要实现MouseMotionListener而不是MouseListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM