swing—有没有一种方法可以通过java中的子类禁用继承函数的一部分?

ylamdve6  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(385)

我正在用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);
    }
}
u7up0aaq

u7up0aaq1#

父类的方法是 public 我看不出为什么你不能简单地覆盖它们并决定你想做什么:
父.java:

public class Parent implements MouseListener {
    @Override  
    public void mouseEntered(MouseEvent e) {
        // all your logic here to highlight when mouse entered/hovered
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // all your logic here for clearing the highlight when the mouse has exited or is not hovering over the component
    }
}

child.java文件:

public class Child extends Parent {
    @Override  
    public void mouseEntered(MouseEvent e) {
        // we call super here as we want to inherit logic of the parent class for highlighting on mouse entered/hover
        super.mouseEntered(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // we dont call super here as we don't want to inherit the parents behavior
    }
}
kx1ctssn

kx1ctssn2#

我解决了这个问题。在子类中,在方法体中重新引入您不想更改的属性,并将其写在mouseexited方法的顶部,并将体留空。然后在else部分调用super.mouseentered(e)函数。这样,在顶部条件将得到满足,并且由于主体是空的,所以不会有任何改变。这是密码
父类:

public void mouseEntered(MouseEvent e) {
    if (e.getSource() == dashboard) {
        dashboard.setBackground(new Color(0x13A89E));
        dashboard.setForeground(Color.white);
    }
    else if (e.getSource() == regCard) {
        regCard.setBackground(new Color(0x13A89E));
        regCard.setForeground(Color.white);
    }
    else if (e.getSource() == fees) {
        fees.setBackground(new Color(0x13A89E));
        fees.setForeground(Color.white);
    }
    else if (e.getSource() == resultCard) {
        resultCard.setBackground(new Color(0x13A89E));
        resultCard.setForeground(Color.white);
    }
    else if (e.getSource() == profile) {
        profile.setBackground(new Color(0x13A89E));
        profile.setForeground(Color.white);
    }
    else if (e.getSource() == logOut) {
        logOut.setBackground(new Color(0x13A89E));
        logOut.setForeground(Color.white);
    }
}

@Override
public void mouseExited(MouseEvent e) {
    if (e.getSource() == dashboard) {
        dashboard.setBackground(Color.WHITE);
        dashboard.setForeground(Color.black);
    }
    else if (e.getSource() == regCard) {
        regCard.setBackground(Color.WHITE);
        regCard.setForeground(Color.black);
    }
    else if (e.getSource() == fees) {
        fees.setBackground(Color.WHITE);
        fees.setForeground(Color.black);
    }
    else if (e.getSource() == resultCard) {
        resultCard.setBackground(Color.WHITE);
        resultCard.setForeground(Color.black);
    }
    else if (e.getSource() == profile) {
        profile.setBackground(Color.WHITE);
        profile.setForeground(Color.black);
    }
    else if (e.getSource() == logOut) {
        logOut.setBackground(Color.WHITE);
        logOut.setForeground(Color.black);
    }
}

儿童班:

public void mouseExited(MouseEvent e) {
    if (e.getSource() == dashboard) {
        //Do nothing
    } else {
        super.mouseExited(e);
    }
}

相关问题