JLabel的工具提示文本禁用其父JPanel mouseEnter事件

时间:2017-08-01 23:29:54

标签: java mouseevent

为什么JLabel的工具提示事件会禁用其JPanel的mouseEnter事件? 如果JLabel没有工具提示文本集,则不会发生这种情况。

如果将鼠标悬停在JLabel上,我怎样才能保留mouseEnter事件?

正常状态,鼠标未超过JPanel enter image description here

悬停状态,鼠标悬停在JPanel上,它将颜色变为红色 enter image description here

悬停状态,鼠标在JLabel上,在JPanel内部

背景应为红色。 enter image description here

代码:

public class MyJFrame extends JFrame {

    private final Color default_color = Color.orange;
    private final Color hover_color = Color.red;

    private JLabel jLabel1;
    private JPanel jPanel1;

    public MyJFrame() {
        init();
    }

    private void init() {

        jPanel1 = new JPanel();
        jLabel1 = new JLabel();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jPanel1.setBackground(default_color);
        jPanel1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent evt) {
                jPanel1.setBackground(hover_color);
            }
            @Override
            public void mouseExited(MouseEvent evt) {
                jPanel1.setBackground(default_color);
            }
        });
        jPanel1.setLayout(null);
        jLabel1.setText("?");
        jLabel1.setToolTipText("This label's tooltip text disabling mouseEnter event.");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(313, 0, 27, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(30, 40, 340, 30);

        pack();
    }                                                                
}

0 个答案:

没有答案