[英]Change JButton properties on mouse over
我正在用 swing 做一个计算器应用程序。 我已经更改了 JButton 的背景颜色,但是当单击它时,它的颜色变为蓝色。 我可以在鼠标悬停和单击时手动设置颜色吗? 像 windows 10 默认计算器?
尝试使用这个:
buttonName.setFocusPainted(false);
当您单击按钮时,这将摆脱默认边框。 要让按钮做一些事情,即改变它的背景,你可以为它实现 MouseListener 接口,但我认为对你来说最好的事情是阅读 swing 中的事件处理:) 这是一个链接
您需要首先使用鼠标侦听器检测鼠标在按钮上的移动。您可以使用它的实现版本,称为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);
}
});
注意鼠标点击你可以改变你的动作监听器内的颜色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.