java - 当鼠标光标悬停在 JButton 上时更改 boolean 值

标签 java swing mouse mouseevent jbutton

我有一个按钮,当您将鼠标悬停在上面时,它会从黑色变为灰色,我使用 setRolloverIcon(ImageIcon); 来实现。当鼠标光标悬停在 JButton 上时,是否有任何简单的方法使 boolean 值等于 true,或者我是否必须使用 MouseMotionListener 检查鼠标光标的位置?

最佳答案

Is there any easy way to make a boolean equals to true while the mouse cursor hovers over the JButton

您可以将ChangeListener 添加到ButtonModel,例如

JButton.getModel().addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();
        if (model.isRollover()) {
            //do something with Boolean variable
        } else {

        }
    }
});

关于java - 当鼠标光标悬停在 JButton 上时更改 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16733708/

相关文章:

java - 如何在我的主类中使用通过 Netbeans GUI 构建器创建的按钮?

javascript - 单击事件 - 获取 mousedown 开始而不是 mouseup 的目标

java.lang.NoClassDefFoundError : com/codename1/ui/ComponentSelector

java - java客户端中的elasticsearch geohash facet

java - 将 JTextField 或 JComboBox 的光标移动到开头

java - 从数组更新 JPanel

java - SWIG java : releasing memory allocated in c++

java - 使用日期和时间创建文件名

cocoa - 在核心动画层中处理鼠标的实用方法

c++ - 子类化的 QWidget 和鼠标事件