java - 在鼠标悬停时更改 JButton 属性
问题描述
我正在用 swing 做一个计算器应用程序。我已经更改了 JButton 的背景颜色,但是当单击它时,它的颜色变为蓝色。我可以在鼠标悬停和单击时手动设置颜色吗?像 Windows 10 默认计算器?
解决方案
您需要首先使用鼠标侦听器检测鼠标在按钮上的移动。您可以使用它的实现版本,称为MouseAdapter
button.addMouseMotionListener(new MouseAdapter()//To
detect mouse motion
{
@override
public void mouseEntered(MouseEvent m)//called when mouse
enters first time into the button
{
button.setBackground(Color.blue);
}
@override
public void mouseClicked(MouseEvent m)//called when mouse is clicked[pressed and then released]
{
button.setBackground(//whatever you want);
}
});
注意鼠标点击你可以改变你的动作监听器内的颜色
推荐阅读
- java - 如何从 Java 泛型类中提取类以满足编译器的要求?
- c# - MVC 如何将表单数据序列化为模型?
- tensorflow - tf.dataset、多路径输入和每批映射以加载图像
- matlab - 手动从图像中提取边界并防止所选点重叠
- java - 尝试运行代码时出现 Java 错误:找不到文件
- php - php下载excel文件
- gcc - 如何在 Windows 2000 机器上使用 Go 编译器获取 GCC9.x 或最新的 gcc
- sql-server - 有没有办法在 SQL Server 中将日期格式化为 MMDDYYYY 而不是 YYYYMMDD?
- node.js - 如何在生产中使用 express-rate-limit
- jquery - 关闭一些 ajax 调用的 nprogress