我想制作一个透明的按钮,直到用户用鼠标悬停在它上面,所以我创建了自己的扩展 JButton 的类。我对其进行了测试,它确实使按钮透明并且确实检测到用户何时将鼠标悬停在它上面,但之后它并没有使它变得不透明。我需要用这个代码改变什么?
import javax.swing.*;
import java.awt.event.*;
public class TransparentButton extends JButton {
boolean opaque = false, areaFilled = false, borderPainted = false;
public TransparentButton(Icon icon) {
super(icon);
initialise();
}
public TransparentButton(String text) {
super(text);
initialise();
}
private void initialise() {
super.setOpaque(opaque);
super.setContentAreaFilled(areaFilled);
super.setBorderPainted(borderPainted);
super.addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
opaque = true;
areaFilled = true;
borderPainted = true;
}
public void mouseExited(MouseEvent e) {
opaque = false;
areaFilled = false;
borderPainted = false;
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
}
}