我有一个按钮,当您将鼠标悬停在上面时,它会从黑色变为灰色,我使用 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/