摆动鼠标听众被儿童组件拦截 [英] swing mouse listeners being intercepted by child components

查看:102
本文介绍了摆动鼠标听众被儿童组件拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个子组件的swing组件。如果鼠标位于任何组件之上,我想要做的是更改某些标签,如果鼠标移出所有组件,则将其更改为其他标签。我正试图找到一种更有效的方法来做到这一点。

I have a swing component that has several sub components. What I want to do change some label if the mouse is over any of those components, and then change it to something else if the mouse moves off all of the components. I'm trying to find a more efficient way to do this.

目前我对所有子组件都有鼠标监听器,如下所示:

Currently I have mouse listeners over all of the child components that look something like:

class AMouseListener extends MouseAdapter {
    private boolean mouseOver;
    mouseEntered(MouseEvent e) { mouseOver = true; updateLabel(); }
    mouseExited(MouseEvent e) { mouseOver = false; updateLabel(); }

    void updateLabel() {
       String text = "not-over-any-components";
       // listeners are each of the listeners added to the child components
       for ( AMouseListener listener :listeners ) {
          if ( listener.mouseOver ) {
             text = "over-a-component";
             break;
          }
       }
    }
}

这个工作,但我觉得应该有一个更好的方法来处理这个只通过处理父容器上的mouseEntered和mouseExited事件,但由于子组件拦截这些事件,我不知道如何去做(我不喜欢我必须控制子组件,所以如果我愿意,我不能将鼠标事件转发给父事件。

This works, but I feel like there should be a better way to handle this by only handling mouseEntered and mouseExited events on the parent container, but since the child components intercept these events, I'm not sure how to go about doing this (I don't necessarily have control over the child components so I Can't forward the mouse events to the parent event if I wanted to).

推荐答案

例如

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class TestMouseListener {

    public static void main(String[] args) {
        final JComboBox combo = new JComboBox();
        combo.setEditable(true);
        for (int i = 0; i < 10; i++) {
            combo.addItem(i);
        }
        final JLabel tip = new JLabel();
        tip.setPreferredSize(new Dimension(300, 20));
        JPanel panel = new JPanel();
        panel.add(combo);
        panel.add(tip);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        panel.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                tip.setText("Outside combobox");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Component c = SwingUtilities.getDeepestComponentAt(
                   e.getComponent(), e.getX(), e.getY());
                // doesn't work if you move your mouse into the combobox popup
                tip.setText(c != null && SwingUtilities.isDescendingFrom(
                   c, combo) ? "Inside combo box" : "Outside combobox");
            }
        });
    }

    private TestMouseListener() {
    }
}

这篇关于摆动鼠标听众被儿童组件拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆