如何在复合材料上跟踪鼠标EXIT / ENTER / HOVER?

下面的代码显示:

1)复合完全忽略了HOVER.如何启用?

2)跟踪ENTER / EXIT,但当鼠标进入子控制区域时,父复合接收EXIT事件.如何使复合材料的这个中心区域成为复合材料?

public class TryHover {

    public static void main(String[] args) {

        Display display = new Display();

        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setLayout(new GridLayout(1, false));

        Label label1 = new Label(composite, SWT.BORDER);
        label1.setText("Label 1");

        Label label2 = new Label(composite, SWT.BORDER);
        label2.setText("Label 2");

        composite.addListener(SWT.MouseEnter, new Listener() {

            @Override
            public void handleEvent(Event event) {
                System.out.println("ENTER");

            }
        });

        composite.addListener(SWT.MouseExit, new Listener() {

            @Override
            public void handleEvent(Event event) {
                System.out.println("EXIT");

            }
        });

        composite.addListener(SWT.MouseHover, new Listener() {

            @Override
            public void handleEvent(Event event) {
                System.out.println("HOVER");

            }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

    }
}

UPDATE

是的,同意,如果鼠标很慢,它也会产生HOVER.

然后问题是关于ENTER / EXIT:如何关闭子控件的EXIT?

最佳答案 SWT.MouseHover仅在鼠标位于Composite上方时触发,并且不会移动几分之一秒.

鼠标在Composite上的每次移动都会触发SWT.MouseMove.

所以这真的取决于你想要达到的目标.

至于问题的第二部分:SWT不会在窗口小部件层次结构中传播事件(有一些例外).

但是,您可以实现自己的逻辑来确定何时收听事件.

>对于SWT.MouseExit:获取鼠标的坐标并检查是否有任何子项包含鼠标.如果是这样,什么都不做,如果没有,你就离开了复合材料.
>对于SWT.MouseEnter:这有点棘手.我想出了一个解决方案,它使用显示屏上的过滤器跟踪鼠标移动的当前Widget.输入Composite时,可以检查上一个窗口小部件是否为子项.如果是这样,什么也不做.

private static Widget mouseControl = null;

public static void main(String[] args)
{
    Display display = new Display();

    /* Overall, keep track of the Widget the mouse is moving over */
    display.addFilter(SWT.MouseMove, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            mouseControl = e.widget;
        }
    });

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new GridLayout(1, false));

    Label label1 = new Label(composite, SWT.BORDER);
    label1.setText("Label 1");

    Label label2 = new Label(composite, SWT.BORDER);
    label2.setText("Label 2");

    composite.addListener(SWT.MouseEnter, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            /* Check if the mouse was previously moving over a child (you could
             * even do recursive search here) */
            for (Control child : composite.getChildren())
            {
                if (child.equals(mouseControl))
                    return;
            }
            System.out.println("ENTER");
        }
    });

    composite.addListener(SWT.MouseExit, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            /* Check if the mouse is now located over a child (you could
             * even do recursive search here) */
            for (Control child : composite.getChildren())
            {
                if (child.getBounds().contains(new Point(event.x, event.y)))
                    return;
            }
            System.out.println("EXIT");
        }
    });

    composite.addListener(SWT.MouseHover, new Listener()
    {

        @Override
        public void handleEvent(Event event)
        {
            System.out.println("HOVER");
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
}
点赞