Java MouseEvents 不工作
这可能是个愚蠢的问题,但我不得不问!
我有以下代码片段,当用户与对象交互时,它们应该运行相应的方法。 由于某种原因,“foo”永远不会被打印,但“bar”却会被打印。
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 中缺少什么? 提前致谢。
编辑: MouseEntered 在以完全相同的方式实现的 JCheckBox 上完美运行!
This may be a stupid question, but I have to ask!
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
}
});
I get no exceptions or stack trace. What am I missing in the MouseListener one?
Thanks in advance.
EDIT: MouseEntered works perfectly on a JCheckBox implemented in exactly the same way!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSpinner
是一个复合组件,由一个文本字段和 2 个按钮组成。通过迭代 getComponents() 的结果并向每个组件添加一个侦听器,可以向所有这些侦听器添加鼠标侦听器。然而,根据我的经验,当某件事需要做那么多工作时,你可能会采取错误的方式。
为什么
JSpinner
需要鼠标输入的信息?您想通过这次活动做什么?
更新:
如果您希望提供有关面板中所有控件的信息,您可能需要考虑使用玻璃板来检测鼠标下方的组件。
Alexander Potochkin 的 行为良好的 Glasspane 是很好的起点。
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 ofgetComponents()
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.
Why do you need the mouse-entered information for a
JSpinner
?What do you want to do with this event?
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.
A Well-behaved Glasspane by Alexander Potochkin is a good place to start.
这是一个猜测,但我怀疑您需要将
MouseListener
添加到JSpinner
的编辑器(通过调用getEditor()
)。我想象编辑器Component
占据了JSpinner
内的所有可用空间,因此拦截了所有MouseEvent
。This is a guess but I suspect you need to add a
MouseListener
to theJSpinner
's editor (via a call togetEditor()
). I imagine that the editorComponent
occupies all available space within theJSpinner
and is therefore intercepting allMouseEvent
s.这对我有用。
由于我们的软件要求,我需要这个来唤起弹出键对话框以增加可用性。
This worked for me.
I needed this in order to evoke a popup key dialog for added usability due to our software requirements.
除了@Rapier回答...
如果您使用类似的东西更改Spinner,
您将丢失以前的MouseListener...
如果您需要更改
SpinnerModel
的某些内容,请不要创建新的,更改其参数代替! (如果这样做,您将需要再次重新分配 MouseListener,因为当您分配新的 SpinnerModel 时它将丢失)。一个例子(我在说......):
Aditional to @Rapier answer...
If you change the Spinner using something like
you will lost your previosly MouseListener...
If you need to change something of
SpinnerModel
, Don't create a new, change its parameters instead! (if you do it, you will need to reassign the MouseListener again, because it will be lost when you assign a new SpinnerModel).an example (I'm talking...):