我正在做一个带有 Swing 的计算器应用程序。我已经改变了 JButton 的背景颜色,但是当点击它的颜色会变成蓝色。我可以在鼠标悬停和单击时手动设置颜色吗?喜欢 Windows 10 默认的计算器吗?
答案 0 :(得分:0)
试试这个:
buttonName.setFocusPainted(false);
当您单击按钮时,这将摆脱默认边框。 为了让按钮做一些事情,即改变它的背景,你可以为它实现 MouseListener 接口,但我认为对你来说最好的事情是阅读 Swing 中的事件处理 :) 这是link
答案 1 :(得分:0)
您需要首先使用 Mouse listener 检测按钮上的鼠标移动。您可以使用它的实现版本,称为 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);
}
});
注意鼠标点击,您可以更改动作侦听器内的颜色