我想製作一個透明的按鈕,直到用戶將鼠標移動到它上面爲止,所以我創建了自己的類來擴展JButton。我對它進行了測試,它確實使按鈕透明,並且在用戶懸停時發現並檢測到按鈕,但不會使其不透明。我需要使用此代碼更改哪些內容?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) {}
});
}
}
謝謝。這很有道理! – sticks 2012-04-06 08:49:59