简体   繁体   English

如何在swing应用程序中获取鼠标指针组件

[英]how to get mouse pointer component in swing application

my swing application one panel have 6 button. 我的挥杆应用程序的一个面板有6个按钮。 when cursor goes on the button i want to change default cursor to hand cursor and cursor is exit then it want to change default cursor. 当光标移到按钮上时,我想将默认光标更改为手形光标,并且光标退出了,那么它想要更改默认光标。 Now i am doing this thing using below code. 现在我正在使用下面的代码来做这个事情。

private void btnRegisterReceiptMouseEntered(java.awt.event.MouseEvent evt) {                                                
    btnRegisterReceipt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}                                               

private void btnRegisterReceiptMouseExited(java.awt.event.MouseEvent evt) {                                               
    btnRegisterReceipt.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}  

Now i want to write this code to each and every button. 现在,我想将此代码写入每个按钮。

But I want to write common method to do this one. 但是我想写一个通用的方法来做到这一点。 I already try to use MouseListener do this thing, but I can not get which is the mouse point component. 我已经尝试使用MouseListener做到这一点,但是我无法获得哪个是鼠标指针组件。

I don't know it is possible or not. 我不知道有没有可能。 if it is possible please anyone tell me how to do this things. 如果有可能,请任何人告诉我该怎么做。

private void changeCursor() {
    addMouseListener(new MouseAdapter() {

        @Override
        public void mouseEntered( MouseEvent e ) {
            /*if ( mouse Entered compornent is button ) {
                button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            } else {
                button.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }*/
        }

    });
}

Write a generic MouseListener (as an annonymouse class): 编写一个通用的MouseListener(作为匿名类):

MouseListener ml = new MouseAdapter()
{
    @Override
    public void mouseEntered(MouseEvent e)
    {
        e.getComponent( setCursor(...) );
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        e.getComponent( setCursor(...) );
    }
};

Then you can just add the MouseListener to any component you want with: 然后,您可以将MouseListener添加到所需的任何组件中:

btnRegisterReceipt.addMouseListener( ml );
anotherButton.addMouseListener( ml );

You can also make this as a reusable class: 您也可以将此作为可重用的类:

public MousePointerListener extends MouseAdapter
{
    @Override
    public void mouseEntered(MouseEvent e)
    {
        e.getComponent( setCursor(...) );
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        e.getComponent( setCursor(...) );
    }
}

Then you use: 然后您使用:

MouseListener ml = new MousePointerListener();
btnRegisterReceipt.addMouseListener( ml );
anotherButton.addMouseListener( ml );

The key point in both examples is that you can get the source of the event from the MouseEvent, which allows you to write generic code. 这两个示例中的关键点是,您可以从MouseEvent获取事件源,从而可以编写通用代码。 You should look at this approach for all you listeners, instead of letting your IDE generate the listener code. 您应该为所有侦听器使用这种方法,而不是让IDE生成侦听器代码。

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

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