不工作的Java MouseEvents [英] Java MouseEvents not working

查看:162
本文介绍了不工作的Java MouseEvents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我必须要问!

This may be a stupid question, but I have to ask!

我有应该运行其相应的方法,当用户与对象进行交互以下code片段。
出于某种原因,富从不打印,但巴是什么。

I have the following code snippets that are supposed to run their corresponding methods when the user interacts with objects. For some reason, "foo" is never printed, but "bar" is.

myJSpinner1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseEntered(java.awt.event.MouseEvent evt) {
    System.out.println("foo"); //"foo" is not printed
  }
});

myJSpinner2.addChangeListener(new java.awt.event.ChangeListener() {
    public void stateChanged(java.awt.event.ChangeEvent evt) {
    System.out.println("bar"); //"bar" is printed
  }
});

我没有得到任何异常或堆栈跟踪。那我在的MouseListener一人失踪?
先谢谢了。

I get no exceptions or stack trace. What am I missing in the MouseListener one? Thanks in advance.

编辑:工作的mouseEntered完美在完全相同的方式实现了一个JCheckBox的

MouseEntered works perfectly on a JCheckBox implemented in exactly the same way!

推荐答案

的JSpinner 是由一个文本框和2个按钮的复合成分。这可以通过迭代的 getComponents()的结果,并添加监听到每个鼠标监听器添加到所有这些的。

JSpinner is a composite component consisting of a text field and 2 buttons. It's possible to add mouse listeners to all of those by iterating over the results of getComponents() and adding a listener to each.

不过,根据我的经验,当事情发生这么多的工作,你可能会约了错误的方式。

However, in my experience, when something takes that much work, you're probably going about it the wrong way.

为什么你需要一个的JSpinner ?结果鼠标输入的信息
你要什么与此事件怎么办?

Why do you need the mouse-entered information for a JSpinner?
What do you want to do with this event?

更新:
如果你正在寻找提供所有在面板中的控件的信息,你可能想看看使用的glassPane检测鼠标下的组件。

Update: If you're looking to supply information about all of the controls in your panel, you may want to look at using a glasspane to detect the component under the mouse.

乖巧的glassPane 通过亚历山大Potochkin是开始的好地方。

A Well-behaved Glasspane by Alexander Potochkin is a good place to start.

这篇关于不工作的Java MouseEvents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆