Java MouseEvents 不工作

发布于 2024-08-27 09:28:13 字数 629 浏览 12 评论 0原文

这可能是个愚蠢的问题,但我不得不问!

我有以下代码片段,当用户与对象交互时,它们应该运行相应的方法。 由于某种原因,“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听你说爱我 2024-09-03 09:28:13

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 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.

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.

沉溺在你眼里的海 2024-09-03 09:28:13

这是一个猜测,但我怀疑您需要将 MouseListener 添加到 JSpinner 的编辑器(通过调用 getEditor())。我想象编辑器 Component 占据了 JSpinner 内的所有可用空间,因此拦截了所有 MouseEvent

This is a guess but I suspect you need to add a MouseListener to the JSpinner's editor (via a call to getEditor()). I imagine that the editor Component occupies all available space within the JSpinner and is therefore intercepting all MouseEvents.

や莫失莫忘 2024-09-03 09:28:13

这对我有用。

JSpinner spinner = new JSpinner();

((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addMouseListener(
    new java.awt.event.MouseAdapter() {            
    public void mouseClicked(final MouseEvent e) {   
        // add code here
    }
});

由于我们的软件要求,我需要这个来唤起弹出键对话框以增加可用性。

This worked for me.

JSpinner spinner = new JSpinner();

((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addMouseListener(
    new java.awt.event.MouseAdapter() {            
    public void mouseClicked(final MouseEvent e) {   
        // add code here
    }
});

I needed this in order to evoke a popup key dialog for added usability due to our software requirements.

时间海 2024-09-03 09:28:13

除了@Rapier回答...

如果您使用类似的东西更改Spinner,

yourOldSpinner = new JSpinner(new SpinnerModel(...))

您将丢失以前的MouseListener...

如果您需要更改SpinnerModel的某些内容,请不要创建新的,更改其参数代替! (如果这样做,您将需要再次重新分配 MouseListener,因为当您分配新的 SpinnerModel 时它将丢失)。

一个例子(我在说......):

((SpinnerNumberModel)yourOldSpinner.getModel()).setValue(size/3);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMinimum(0);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMaximum(isize/2);
((SpinnerNumberModel)yourOldSpinner.getModel()).setStepSize(1);

Aditional to @Rapier answer...

If you change the Spinner using something like

yourOldSpinner = new JSpinner(new SpinnerModel(...))

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...):

((SpinnerNumberModel)yourOldSpinner.getModel()).setValue(size/3);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMinimum(0);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMaximum(isize/2);
((SpinnerNumberModel)yourOldSpinner.getModel()).setStepSize(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文