我正在用java构建一个gui项目。在父类中,我实现了mouselistener,当光标悬停在按钮上时高亮显示按钮,当光标没有悬停时停止。我想在子类中禁用它,这样选中的按钮就会永久高亮显示,表明它已被选中。在child类中,当我将鼠标悬停在它上面时,它会再次改变颜色。我想从子类中删除函数的特定部分,但我必须重新编写它来重写函数。有没有什么方法可以在child类中禁用该部分?
下面是代码片段
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
if (e.getSource() == notifications) {
notifications.setBackground(new Color(0x13A89E));
}
if (e.getSource() == attendance) {
attendance.setBackground(new Color(0x13A89E));
}
if (e.getSource() == marks) {
marks.setBackground(new Color(0x13A89E));
}
if (e.getSource() == learning) {
learning.setBackground(new Color(0x13A89E));
}
if (e.getSource() == assignments) {
assignments.setBackground(new Color(0x13A89E));
}
if (e.getSource() == GDB) {
GDB.setBackground(new Color(0x13A89E));
}
if (e.getSource() == MDB) {
MDB.setBackground(new Color(0x13A89E));
}
if (e.getSource() == quizzes) {
quizzes.setBackground(new Color(0x13A89E));
}
if (e.getSource() == lectureContents) {
lectureContents.setBackground(new Color(0x13A89E));
}
if (e.getSource() == courseInformation) {
courseInformation.setBackground(new Color(0x13A89E));
}
}
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
if (e.getSource() == notifications) {
notifications.setBackground(null);
}
if (e.getSource() == attendance) {
attendance.setBackground(null);
}
if (e.getSource() == marks) {
marks.setBackground(null);
}
if (e.getSource() == learning) {
learning.setBackground(null);
}
if (e.getSource() == assignments) {
assignments.setBackground(null);
}
if (e.getSource() == GDB) {
GDB.setBackground(null);
}
if (e.getSource() == MDB) {
MDB.setBackground(null);
}
if (e.getSource() == quizzes) {
quizzes.setBackground(null);
}
if (e.getSource() == lectureContents) {
lectureContents.setBackground(null);
}
if (e.getSource() == courseInformation) {
courseInformation.setBackground(null);
}
}
2条答案
按热度按时间u7up0aaq1#
父类的方法是
public
我看不出为什么你不能简单地覆盖它们并决定你想做什么:父.java:
child.java文件:
kx1ctssn2#
我解决了这个问题。在子类中,在方法体中重新引入您不想更改的属性,并将其写在mouseexited方法的顶部,并将体留空。然后在else部分调用super.mouseentered(e)函数。这样,在顶部条件将得到满足,并且由于主体是空的,所以不会有任何改变。这是密码
父类:
儿童班: