为什么JLabel
的工具提示事件会禁用其JPanel的mouseEnter事件?
如果JLabel没有工具提示文本集,则不会发生这种情况。
如果将鼠标悬停在JLabel上,我怎样才能保留mouseEnter事件?
悬停状态,鼠标在JLabel上,在JPanel内部
代码:
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();
}
}