当鼠标光标悬停在JButton上时,更改布尔值 [英] Changing value of boolean when mouse cursor hovers over a JButton
本文介绍了当鼠标光标悬停在JButton上时,更改布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当你将鼠标悬停在我的按钮上时,它会从黑色变为灰色,我使用 setRolloverIcon(ImageIcon);
执行此操作。当鼠标光标悬停在JButton上时,是否有任何简单的方法可以使布尔值等于true,或者我是否必须使用MouseMotionListener来检查鼠标光标的位置?
I have a button that will change from black to gray when you hover over, I do this with setRolloverIcon(ImageIcon);
. Is there any easy way to make a boolean equals to true while the mouse cursor hovers over the JButton or would I have to use a MouseMotionListener to check the position of the mouse cursor?
推荐答案
当鼠标
光标悬停在JButton上时,有没有简单的方法使布尔值等于true
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 {
}
}
});
这篇关于当鼠标光标悬停在JButton上时,更改布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文