Java JCheckbox 更改侦听器收到鼠标悬停事件的通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1302714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:01:25  来源:igfitidea点击:

JCheckbox change listener gets notified of mouse over events

javaeventsevent-handlingcheckboxlistener

提问by Savvas Dalkitsis

Can some one please explain to me why this piece of code prints out to the console when you move your mouse over the check box? What is the "change" event that takes place?

有人可以向我解释为什么当您将鼠标移到复选框上时这段代码会打印到控制台吗?发生的“变化”事件是什么?

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JCheckBox c = new JCheckBox("Print HELLO");
        c.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("HELLO");
            }
        });
        f.getContentPane().add(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

}

NOTE: I don't use an action listener because in my program i want to be able to do :

注意:我不使用动作侦听器,因为在我的程序中我希望能够做到:

checkBox.setSelected(boolean)

and have my listener notified, which can't be done with an action listener. So is there a way to disable this "mouse over" event or another way i can implement my listener?

并通知我的侦听器,这是动作侦听器无法完成的。那么有没有办法禁用这个“鼠标悬停”事件或者我可以实现我的监听器的另一种方式?

采纳答案by Aaron

You get events on mouse over as focus gained/lost represents a change to the state of the component.

当焦点获得/失去代表组件状态的变化时,您会在鼠标悬停时获得事件。

Instead you could use an ItemListener which will give you ItemEvents.

相反,您可以使用 ItemListener 来为您提供 ItemEvents。

The object that implements the ItemListener interface gets this ItemEvent when the event occurs. The listener is spared the details of processing individual mouse movements and mouse clicks, and can instead process a "meaningful" (semantic) event like "item selected" or "item deselected".

实现 ItemListener 接口的对象在事件发生时获取这个 ItemEvent。侦听器无需处理单独的鼠标移动和鼠标单击的细节,而是可以处理“有意义的”(语义)事件,例如“项目选择”或“项目取消选择”。

You can add it to your checkbox with the addItemListener() method in the AbstractButton class. Just replace addChangeListener with this:

您可以使用 AbstractButton 类中的 addItemListener() 方法将其添加到您的复选框中。只需用这个替换 addChangeListener :

c.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e) {
        System.err.println(e.getStateChange());
    }
});

回答by Tom Hawtin - tackline

The state of the check box (even just the check box model) changes depending upon whether it has the mouse over it or not. So a state change event should be expected.

复选框的状态(甚至只是复选框模型)会根据鼠标是否悬停在上面而发生变化。因此应该预期会发生状态更改事件。

So, just check back to see what state the check box is in and update accordingly. It is better to go straight for the model, rather than using the "bloated" component interface.

因此,只需返回查看复选框处于什么状态并进行相应更新。最好直接上模型,而不是使用“臃肿”的组件接口。

回答by Fredrik

Use c.setRolloverEnabled(false)` and you won't get any event for mouse motions.

使用c.setRolloverEnabled(false)` 并且您不会收到任何鼠标移动事件。