Java:mouseMoved事件和button.addMouseListener是否会干扰?

时间:2013-08-27 21:01:18

标签: java events cursor

现在我使用以下代码绘制光标(具有更大的尺寸):

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);

之后我使用以下代码作为按钮:

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) {
                } 
            });

光标工作正常,但只有当我没有按下按钮时,因为它比在按钮下面绘制..有什么问题?

1 个答案:

答案 0 :(得分:0)

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

也许您可以使用AWTEventListener来监听鼠标事件。有关详细信息,请参阅Global Event Listeners