简体   繁体   English

在底层组件上触发鼠标事件

[英]Fire mouse event on underlying components

I'm looking for a way to pass mouse events to components covered by other components.我正在寻找一种将鼠标事件传递给其他组件覆盖的组件的方法。 To illustrate what I mean, here's a sample code.为了说明我的意思,这里有一个示例代码。 It contains two JLabel s, one is twice smaller and entirely covered with a bigger label.它包含两个JLabel ,一个小两倍,完全被一个更大的标签覆盖。 If you mouse over the labels, only the bigger one fires mouseEntered event however.如果您将鼠标悬停在标签上,则只有较大的标签才会触发mouseEntered事件。

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

public class MouseEvtTest extends JFrame {

    public MouseEvtTest() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
        setSize(250, 250);

        MouseAdapter listener = new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.printf("Mouse entered %s label%n", e.getComponent().getName());
            }
        };
        LineBorder border = new LineBorder(Color.BLACK);

        JLabel smallLabel = new JLabel();
        smallLabel.setName("small");
        smallLabel.setSize(100, 100);
        smallLabel.setBorder(border);
        smallLabel.addMouseListener(listener);
        add(smallLabel);

        JLabel bigLabel = new JLabel();
        bigLabel.setName("big");
        bigLabel.setBorder(border);
        bigLabel.setSize(200, 200);
        bigLabel.addMouseListener(listener);
        add(bigLabel, 0); //Add to the front
    }

    public static void main(String[] args) {
        new MouseEvtTest().setVisible(true);
    }
}

What would be the best way to fire mouse entered event on the smaller label when cursor is moved to the coordinates above it?当光标移动到其上方的坐标时,在较小的标签上触发鼠标输入事件的最佳方法是什么? How would it work in case where there would be multiple components stacked on top of each other?如果多个组件堆叠在一起,它将如何工作? What about the remaining mouse events, like mouseClicked , mousePressed , mouseReleased , etc.?剩下的鼠标事件如何处理,比如mouseClickedmousePressedmouseReleased等?

In your listener:在您的听众中:

bigLabel.dispatchEvent(mouseEvent);

Of course, you will have to define bigLabel as final当然,您必须将bigLabel定义为final

看看 Alexander Potochkin 在A Well-Behaved GlassPane上的博客条目

Well to understand whats happening you need to understand how Z-Ordering works.要了解正在发生的事情,您需要了解 Z-Ordering 的工作原理。 As a quick overview, the component that was added last is painted first.作为快速概览,最后添加的组件首先被绘制。 So in your case you want to add the small component before the big component.因此,在您的情况下,您希望在大组件之前添加小组件。

// add(bigLabel, 0); //Add to the front
add(bigLabel); // add to the end so it is painted first

The OverlayLayout might help explain this better and give you another option. OverlayLayout可能有助于更好地解释这一点并为您提供另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM