Java:mouseMoved事件和button.addMouseListener是否干扰? [英] Java: does a mouseMoved event and a button.addMouseListener interfere?

查看:423
本文介绍了Java:mouseMoved事件和button.addMouseListener是否干扰?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用下面的代码来绘制一个更大的游标:

Now I am using the following code to draw a cursor (with a greater size):

Cursor emptyCursor = Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), "empty"); 
        setCursor(emptyCursor);

        Toolkit toolkit = Toolkit.getDefaultToolkit();

        final long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK + AWTEvent.KEY_EVENT_MASK;
        final ImageIcon icon = createImageIcon("images/cursor.png");
        cursorLabel = new JLabel(icon);

        toolkit.addAWTEventListener(new AWTEventListener() {
            public void eventDispatched(AWTEvent e) {
                MouseEvent  me=(MouseEvent)e;
                cursorLabel.setLocation(me.getLocationOnScreen().x, me.getLocationOnScreen().y);
            }
        }, eventMask);

        layeredPane = this.getLayeredPane();  
        if (icon != null) {
            cursorLabel.setBounds(15, 225, icon.getIconWidth(), icon.getIconHeight());
        } else {
            System.err.println("Cursor Icon not found!");
        }
        layeredPane.add(cursorLabel);

之后,我使用下面的按钮代码:

Afterwards I used the following code for the button:

button.addMouseListener(new MouseListener(){
                public void mouseClicked(MouseEvent arg0) {
                }
                public void mouseEntered(MouseEvent arg0) {
                    button.setBackground(Color.yellow);
                }
                public void mouseExited(MouseEvent arg0) {
                    button.setBackground(Color.white);
                }
                public void mousePressed(MouseEvent arg0) {
                }
                public void mouseReleased(MouseEvent arg0) {
                } 
            });

光标工作正常,但只有当我不按按钮,按钮..有什么问题?

The cursor works fine, but only if I don't press the button, because than it's drawn under the button.. What's the problem?

推荐答案

按钮有自己的MouseListener来处理翻转效果。所以MouseEvent只传递给按钮而不是分层的窗格。

Buttons have their own MouseListener to handle rollover effects. So the MouseEvent is only passed to the button and not the layered pane.

也许你可以使用AWTEventListener监听你的鼠标事件。有关详情,请参见全球性活动监听器

Maybe you can use an AWTEventListener to listen for your mouse events. See Global Event Listeners for more information.

这篇关于Java:mouseMoved事件和button.addMouseListener是否干扰?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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