简体   繁体   English

在JButton上使用鼠标运动监听器?

[英]Using mouse Motion Listener on JButton?

hii I have used Image as JButton for set in to panel but now i want to use mouse motion listener actions on that image for that purpose what can i do hii我已经使用Image作为JButton来设置到面板中,但是现在我想为此目的在该图像上使用鼠标运动侦听器动作,我该怎么办

following is the code for image 以下是图像的代码

JButton buttonForPicture = new JButton();

    buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
    buttonForPicture.setOpaque(false);
    buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg"));
    panel5.add(buttonForPicture,BorderLayout.CENTER);

is there any another way to do that or ... 还有其他方法可以做到吗?

I'm not sure exactly what you're asking? 我不确定您要问什么?

Your button is like any other JButton: 您的按钮就像其他JButton一样:

buttonForPicture.addMouseMotionListener(new MouseMotionListener() {
    @Override
    public void mouseMoved(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }
});

That catches movement events for the whole button, not just the image. 这捕获了整个按钮的运动事件,而不仅仅是图像。

Read the JButton API there are methods to change the icon on a mouse rollover, if thats what you are trying to do. 阅读JButton API,有很多方法可以改变鼠标悬停时的图标,如果您要这样做的话。 Search the API for methods containing "icon" to see what your options are. 在API中搜索包含“ icon”的方法,以查看您的选择。

If you just want to know how to write a MouseMotionListener, then read the section from the Swing tutorail on How to Write a Mouse Motion Listener for a working example. 如果您只想知道如何编写MouseMotionListener,请阅读Swing tutorail中有关如何编写鼠标动作监听器的部分 ,以获取一个有效的示例。

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

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