My FatherPanel
changes colors through mouse entered (RED)/exited (ORANGE) mouse events. It works good, but when I enter the button "Testbutton" (which is a child component of my father panel) the mouse exited event appears. But I am still inside my father panel.
Can somebody explain me why and how to solve such an issue?
I want the father panel to be orange as long as my mouse is inside that panel (no matter if the mouse is on a child-object or not).
public class MainFrame extends JFrame {
public MainFrame() {
FatherPanel fatherPanel = new FatherPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 700);
setLayout(null);
add(fatherPanel);
fatherPanel.setBounds(150, 20, 300, 300);
}
public class FatherPanel extends JPanel{
JButton btn1 = new JButton("Testbutton");
public FatherPanel() {
setSize(300, 300);
setLayout(null);
setBackground(Color.RED);
add(btn1);
btn1.setBounds(150, 150, 100, 100);
addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
setBackground(Color.RED);
}
@Override
public void mouseEntered(MouseEvent e) {
setBackground(Color.ORANGE);
}
@Override
public void mouseClicked(MouseEvent e) {
}
});
}
}
}